自定义简单删减dialog

最近公司比较闲。一闲下来的时候就比较蛋疼,so 趁这个时间整理一下之前到处写的代码。(ps:没办法帐号太多。有的在这个论坛写着有个在那个论坛写着。。比较坑。赶紧搞个博客。省的代码到处都是。^_^!!)

 

今天这个dialog 是之前写的,整理的时候感觉这个用着挺方便的。就整理起来啦!!

 

首先上图:

 

这是一个自定义的dialog,图片  样式 不是重点  嘿嘿 。 我喜欢的其实是只要简简单单两行代码就可以显示和隐藏其中一个item。

上代码先:

  1 package com.hch.customdialog;
  2 
  3 import android.app.Dialog;
  4 import android.content.Context;
  5 import android.view.View;
  6 import android.view.ViewGroup;
  7 import android.view.Window;
  8 import android.view.WindowManager;
  9 import android.widget.Button;
 10 
 11 public class CustomDialog extends Dialog {
 12     //从上到下1、2、3、4四个按钮的监听事件
 13     View.OnClickListener openFirstClickListener;
 14     View.OnClickListener openTwoClickListener;
 15     View.OnClickListener openThreeClickListener;
 16     View.OnClickListener cancelClickListener;
 17     //从上到下1、2、3、4四个按钮的监听事件
 18     private Button custom_dialog_openFirst;
 19     private Button custom_dialog_openTwo;
 20     private Button custom_dialog_openThree;
 21     private Button custom_dialog_cancel;
 22     //从上到下1、2、3、4四个按钮的文字
 23     private String firstText;
 24     private String twoText;
 25     private String threeText;
 26     private String cancelText;
 27     
 28     /*
 29      * 暴漏从上到下1、2、3、4四个按钮的click事件监听
 30      */
 31     public void setOpenFirstClickListener(View.OnClickListener onFirstClickListener){
 32         this.openFirstClickListener = onFirstClickListener;
 33     }
 34     public void setOpenTwoClickListener(View.OnClickListener onTwoClickListener){
 35         this.openTwoClickListener = onTwoClickListener;
 36     }
 37     public void setOpenThreeClickListener(View.OnClickListener onThreeClickListener){
 38         this.openThreeClickListener = onThreeClickListener;
 39     }
 40     public void setOpenCancelClickListener(View.OnClickListener onCancelClickListener){
 41         this.cancelClickListener = onCancelClickListener;
 42     }
 43     
 44     /*
 45      * 暴漏从上到下1、2、3、4四个按钮的文字设置方法
 46      */
 47     public void setFirstButtonText(String firstText){
 48         this.firstText = firstText;
 49     }
 50     public void setTwoButtonText(String twoText){
 51         this.twoText = twoText;
 52     }
 53     public void setThreeButtonText(String threeText){
 54         this.threeText = threeText;
 55     }
 56     public void setCancelButtonText(String cancelText){
 57         this.cancelText = cancelText;
 58     }
 59 
 60     public CustomDialog(Context context) {
 61         super(context, R.style.transparentFrameWindowStyle);
 62         setCanceledOnTouchOutside(true);
 63         setContentView(R.layout.custom_dialog);
 64         initView();
 65         Window window = getWindow();
 66         window.setWindowAnimations(R.style.main_menu_animstyle);
 67         WindowManager.LayoutParams wl = window.getAttributes();
 68         wl.x = 0;
 69         wl.y = getWindow().getWindowManager().getDefaultDisplay().getHeight();
 70         // 以下这两句是为了保证按钮可以水平满屏
 71         wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
 72         wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
 73         this.getWindow().setAttributes(wl);
 74     }
 75     
 76     /**
 77     * @Title: initView
 78     * @Description: TODO 初始化组件
 79     * @author hechuang 
 80     * @date 2015-7-13
 81     * @param     设定文件
 82     * @return void    返回类型
 83     */ 
 84     private void initView(){
 85         custom_dialog_openFirst = (Button)findViewById(R.id.custom_dialog_open_first_bt);
 86         custom_dialog_openTwo = (Button)findViewById(R.id.custom_dialog_open_two_bt);
 87         custom_dialog_openThree = (Button)findViewById(R.id.custom_dialog_open_three_bt);
 88         custom_dialog_cancel = (Button)findViewById(R.id.custom_dialog_open_cancel_bt);
 89     }
 90     @Override
 91     public void show() {
 92         /*
 93          * 第一个按钮事件
 94          */
 95         if(openFirstClickListener != null){
 96             if(firstText != null && firstText.length() > 0){
 97                 custom_dialog_openFirst.setText(firstText);
 98             }
 99             custom_dialog_openFirst.setOnClickListener(new View.OnClickListener() {
100                 
101                 @Override
102                 public void onClick(View v) {
103                     dismiss();
104                     openFirstClickListener.onClick(v);
105                 }
106             });
107         }else{
108             custom_dialog_openFirst.setVisibility(View.GONE);
109         }
110         /*
111          * 第二个按钮事件
112          */
113         if(openTwoClickListener != null){
114             if(twoText != null && twoText.length() > 0){
115                 custom_dialog_openTwo.setText(twoText);
116             }
117             custom_dialog_openTwo.setOnClickListener(new View.OnClickListener() {
118                 
119                 @Override
120                 public void onClick(View v) {
121                     dismiss();
122                     openTwoClickListener.onClick(v);
123                 }
124             });
125         }else{
126             custom_dialog_openTwo.setVisibility(View.GONE);
127         }
128         /*
129          * 第三个按钮事件
130          */
131         if(openThreeClickListener != null){
132             if(threeText != null && threeText.length() > 0){
133                 custom_dialog_openThree.setText(threeText);
134             }
135             custom_dialog_openThree.setOnClickListener(new View.OnClickListener() {
136                 
137                 @Override
138                 public void onClick(View v) {
139                     dismiss();
140                     openThreeClickListener.onClick(v);
141                 }
142             });
143         }else{
144             custom_dialog_openThree.setVisibility(View.GONE);
145         }
146         /*
147          * 取消按钮事件
148          */
149         if(cancelClickListener != null){
150             if(cancelText != null && cancelText.length() > 0){
151                 custom_dialog_cancel.setText(cancelText);
152             }
153             custom_dialog_cancel.setOnClickListener(new View.OnClickListener() {
154                 
155                 @Override
156                 public void onClick(View v) {
157                     dismiss();
158                     cancelClickListener.onClick(v);
159                 }
160             });
161         }else{
162             custom_dialog_cancel.setVisibility(View.GONE);
163         }
164         super.show();
165     }
166 }
View Code

代码很简单,相信大家都能看的懂。

用起来也比较简单:

 1 protected void onCreate(Bundle savedInstanceState) {
 2         super.onCreate(savedInstanceState);
 3         setContentView(R.layout.activity_main);
 4         customDialog = new CustomDialog(MainActivity.this);
 5         findViewById(R.id.but).setOnClickListener(new OnClickListener() {
 6             
 7             @Override
 8             public void onClick(View v) {
 9                 customDialog.setFirstButtonText("aa");
10                 customDialog.setTwoButtonText("bb");
11                 customDialog.setThreeButtonText("cc");
12                 customDialog.setCancelButtonText("取消");
13                 customDialog.setOpenFirstClickListener(new OnClickListener() {
14                     
15                     @Override
16                     public void onClick(View v) {
17                         // TODO Auto-generated method stub
18                         
19                     }
20                 });
21                 customDialog.setOpenTwoClickListener(new OnClickListener() {
22                     
23                     @Override
24                     public void onClick(View v) {
25                         // TODO Auto-generated method stub
26                         
27                     }
28                 });
29                 customDialog.setOpenThreeClickListener(new OnClickListener() {
30                     
31                     @Override
32                     public void onClick(View v) {
33                         // TODO Auto-generated method stub
34                         
35                     }
36                 });
37                 customDialog.setOpenCancelClickListener(new OnClickListener() {
38                     
39                     @Override
40                     public void onClick(View v) {
41                         // TODO Auto-generated method stub
42                         
43                     }
44                 });
45                 customDialog.show();
46             }
47         });
48     }

 

那一个item添加了监听事件那一个item就显示 都否则就不显示。
嗯。就这些。勿喷哈!
对了 附上源码连接,看不懂的小伙伴可以下载下来用。
http://pan.baidu.com/s/1eQdaMrS

 

posted @ 2015-07-13 15:08  蛋蛋嘚忧伤  阅读(140)  评论(0编辑  收藏  举报