码迷,mamicode.com
首页 > 其他好文 > 详细

采用建造者模式自定义对话框

时间:2016-05-24 00:24:09      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

1、对话框视图

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="300dip"
 4     android:layout_height="240dip"
 5     android:orientation="vertical" >
 6     <LinearLayout 
 7         android:layout_width="match_parent"
 8         android:layout_height="200dip"
 9         android:orientation="vertical"
10         >
11         <TextView
12             android:id="@+id/tv_title"
13             android:layout_width="match_parent"
14             android:layout_height="50dip"
15             android:gravity="center"
16             android:background="#00ff00"
17             android:text="提示" />
18         <TextView 
19             android:id="@+id/tv_message"
20             android:layout_width="match_parent"
21             android:layout_height="150dip"
22             android:gravity="center"
23             android:text="对话框"
24             />
25     </LinearLayout>
26 
27     <LinearLayout
28         android:layout_width="match_parent"
29         android:layout_height="fill_parent"
30         android:orientation="horizontal"
31          >
32 
33         <Button 
34             android:id="@+id/btn_confirm"
35             android:layout_width="0dip"
36             android:layout_height="wrap_content"
37             android:layout_weight="1"
38             android:text="确定"
39             />
40         <Button 
41             android:id="@+id/btn_cancel"
42             android:layout_width="0dip"
43             android:layout_height="wrap_content"
44             android:layout_weight="1"
45             android:text="取消"
46             />
47     </LinearLayout>
48 </LinearLayout>

2、对话框Theme

1 <style name="zyhDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
2          <item name="android:windowFrame">@null</item>
3          <item name="android:windowNoTitle">true</item>
4          <item name="android:windowIsTranslucent">true</item>
5          <item name="android:backgroundDimEnabled">false</item>
6     </style>

3、MainActivity

 1 package com.zyhui.zyh_dialog;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.DialogInterface;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.Toast;
 9 
10 public class MainActivity extends Activity {
11     private ZyhDialog zyhDialog;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16     }
17 
18     public void click(View v){
19         ZyhDialog.Builder builder = new ZyhDialog.Builder(this);
20         builder.setTitle("我是dialog的标题 ");
21         builder.setMessage("我是消息内容");
22         builder.setConfirmButton("确定", new DialogInterface.OnClickListener(){
23             @Override
24             public void onClick(DialogInterface dialog, int which) {
25                 Toast.makeText(MainActivity.this, "我点击了确定按钮", Toast.LENGTH_SHORT).show();
26             }});
27         builder.setCancelButton("取消", new DialogInterface.OnClickListener(){
28             @Override
29             public void onClick(DialogInterface dialog, int which) {
30                 Toast.makeText(MainActivity.this, "我点击了取消按钮", Toast.LENGTH_SHORT).show();
31             }
32         });
33         
34         zyhDialog = builder.create();
35         zyhDialog.setCancelable(false);
36         zyhDialog.show();
37     }
38     
39 }

4、自定义对话框类

  1 package com.zyhui.zyh_dialog;
  2 
  3 import android.app.Dialog;
  4 import android.content.Context;
  5 import android.content.DialogInterface;
  6 import android.view.View;
  7 import android.view.ViewGroup.LayoutParams;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10 
 11 public class ZyhDialog extends Dialog {
 12 
 13     public ZyhDialog(Context context, boolean cancelable,
 14             OnCancelListener cancelListener) {
 15         super(context, cancelable, cancelListener);
 16     }
 17 
 18     public ZyhDialog(Context context, int theme) {
 19         super(context, theme);
 20     }
 21 
 22     public ZyhDialog(Context context) {
 23         super(context);
 24     }
 25     
 26     public static class Builder{
 27         private Context context;
 28         
 29         private String title="提示";
 30         private String message="是否确定";
 31         private String confirmText="确定";
 32         private String cancelText="取消";
 33         private DialogInterface.OnClickListener confirmListener;//确认点击事件
 34         private DialogInterface.OnClickListener cancelListener;//取消点击事件
 35         private View dialogView;//对话框视图
 36         
 37         public Builder(Context context){
 38             this.context = context;
 39         }
 40 
 41         public String getTitle() {
 42             return title;
 43         }
 44 
 45         public Builder setTitle(String title) {
 46             this.title = title;
 47             return this;
 48         }
 49 
 50         public String getMessage() {
 51             return message;
 52         }
 53 
 54         public Builder setMessage(String message) {
 55             this.message = message;
 56             return this;
 57         }
 58         //设置确定按钮
 59         public Builder setConfirmButton(String confirmText, DialogInterface.OnClickListener listener){
 60             this.confirmText = confirmText;
 61             this.confirmListener = listener;
 62             return this;
 63         }
 64         
 65         //设置取消按钮
 66         public Builder setCancelButton(String cancelText, DialogInterface.OnClickListener listener){
 67             this.cancelText = cancelText;
 68             this.cancelListener = listener;
 69             return this;
 70         }
 71         
 72         //构建Dialog
 73         public ZyhDialog create(){
 74             dialogView = View.inflate(context, R.layout.zyh_dialog, null);
 75             final ZyhDialog zyhDialog = new ZyhDialog(context,R.style.zyhDialog);
 76             zyhDialog.setContentView(dialogView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
 77             TextView tv_title = (TextView) dialogView.findViewById(R.id.tv_title);
 78             tv_title.setText(getTitle());
 79             TextView tv_message = (TextView) dialogView.findViewById(R.id.tv_message);
 80             tv_message.setText(getMessage());
 81             
 82             Button btn_confirm = (Button) dialogView.findViewById(R.id.btn_confirm);
 83             btn_confirm.setText(confirmText);
 84             if(confirmListener != null){
 85                 btn_confirm.setOnClickListener(new View.OnClickListener() {
 86                     @Override
 87                     public void onClick(View v) {
 88                         confirmListener.onClick(zyhDialog, DialogInterface.BUTTON_POSITIVE);
 89                         zyhDialog.dismiss();
 90                     }
 91                 });
 92             }else{
 93                 btn_confirm.setVisibility(View.GONE);
 94             }
 95             
 96             Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel);
 97             btn_cancel.setText(cancelText);
 98             if(cancelListener != null){
 99                 btn_cancel.setOnClickListener(new View.OnClickListener() {
100                     @Override
101                     public void onClick(View v) {
102                         cancelListener.onClick(zyhDialog, DialogInterface.BUTTON_NEGATIVE);
103                         zyhDialog.dismiss();
104                     }
105                 });
106             }else{
107                 btn_cancel.setVisibility(View.GONE);
108             }
109             return zyhDialog;
110         }
111     }
112 
113 }

 

采用建造者模式自定义对话框

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5521919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!