Android开发必知 九种对话框的实现方法

时间:2021-09-20 06:37:10

在开发过程中,与用户交互式免不了会用到对话框以实现更好的用户体验,所以掌握几种对话框的实现方法还是非常有必要的。在看具体实例之前先对alertdialog做一个简单介绍。alertdialog是功能最丰富、实践应用最广的对话框,它可以生成各种内容的对话框。但实际上alertdialog生成的对话框总体可分为以下4个区域:图标区、标题区、内容区、按钮区。

这里总结了九种对话框的实现方法,有需要的朋友可以来学习下了

 Android开发必知 九种对话框的实现方法

Android开发必知 九种对话框的实现方法

Android开发必知 九种对话框的实现方法

Android开发必知 九种对话框的实现方法

除了popupwindow实现稍微麻烦一点,其他形似都相对简单,熟悉2便即可

直接上源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.naoh.stu;
import java.util.arraylist;
import android.app.activity;
import android.app.alertdialog;
import android.app.progressdialog;
import android.content.dialoginterface;
import android.graphics.drawable.bitmapdrawable;
import android.os.bundle;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup.layoutparams;
import android.view.windowmanager;
import android.widget.button;
import android.widget.edittext;
import android.widget.popupwindow;
import android.widget.toast;
public class diaallactivity extends activity implements runnable {
  private button btn_dianormal; 
  private button btn_diamulti;
  private button btn_dialist;
  private button btn_diasinchos;
  private button btn_diamultichos;
  private button btn_diaprocess;
  private button btn_diareadprocess;
  private button btn_diacustom;
  private button btn_popupdia;
  private popupwindow window=null;
  private button cuspopupbtn1;
  private view popupview;
  @override
  public void oncreate(bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.dialog);
    getview();
    setlistener();
  }
  private void getview()
  {
    btn_dianormal=(button)findviewbyid(r.id.btn_dianormal);
    btn_diamulti=(button)findviewbyid(r.id.btn_diamulti);
    btn_dialist=(button)findviewbyid(r.id.btn_dialist);
    btn_diasinchos=(button)findviewbyid(r.id.btn_diasigchos);
    btn_diamultichos=(button)findviewbyid(r.id.btn_diamultichos);
    btn_diaprocess=(button)findviewbyid(r.id.btn_diaprocess);
    btn_diareadprocess=(button)findviewbyid(r.id.btn_diareadprocess);
    btn_diacustom=(button)findviewbyid(r.id.btn_diacustom);
    btn_popupdia=(button)findviewbyid(r.id.btn_popupdia);
  }
  private void setlistener()
  {
    btn_dianormal.setonclicklistener(btnlistener);
    btn_diamulti.setonclicklistener(btnlistener);
    btn_dialist.setonclicklistener(btnlistener);
    btn_diasinchos.setonclicklistener(btnlistener);
    btn_diamultichos.setonclicklistener(btnlistener);
    btn_diaprocess.setonclicklistener(btnlistener);
    btn_diareadprocess.setonclicklistener(btnlistener);
    btn_diacustom.setonclicklistener(btnlistener);
    btn_popupdia.setonclicklistener(btnlistener);
  }
  private button.onclicklistener btnlistener= new button.onclicklistener()
  {
    public void onclick(view v)
    {
      if(v instanceof button)
      {
        int btnid=v.getid();
        switch(btnid)
        {
          case r.id.btn_dianormal:
            shownormaldia();
            break;
          case r.id.btn_diamulti:
            showmultidia();
            break;
          case r.id.btn_dialist:
            showlistdia();
            break;
          case r.id.btn_diasigchos:
            showsinchosdia();
            break;
          case r.id.btn_diamultichos:
            showmultichosdia();
            break;
          case r.id.btn_diareadprocess:
            showreadprocess();
            break;
          case r.id.btn_diaprocess:
            showprocessdia();
            break;
          case r.id.btn_diacustom:
            showcustomdia();
            break;
          case r.id.btn_popupdia:
            showcuspopup(v);
            break;
          default:
            break;
        }
      }
    }
  };
  /*普通的对话框*/
  private void shownormaldia()
  {
    //alertdialog.builder normaldialog=new alertdialog.builder(getapplicationcontext());
    alertdialog.builder normaldia=new alertdialog.builder(diaallactivity.this);
    normaldia.seticon(r.drawable.ic_launcher);
    normaldia.settitle("普通的对话框");
    normaldia.setmessage("普通对话框的message内容");
    normaldia.setpositivebutton("确定", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        showclickmessage("确定");
      }
    });
    normaldia.setnegativebutton("取消", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        showclickmessage("取消");
      }
    });
    normaldia.create().show();
  }
  /*多按钮对话框*/
  private void showmultidia()
  {
    alertdialog.builder multidia=new alertdialog.builder(diaallactivity.this);
    multidia.settitle("多选项对话框");
    multidia.setpositivebutton("按钮一", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        showclickmessage("按钮一");
      }
    });
    multidia.setneutralbutton("按钮二", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        showclickmessage("按钮二");
      }
    });
    multidia.setnegativebutton("按钮三", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        showclickmessage("按钮三");
      }
    });
    multidia.create().show();
  }
  /*列表对话框*/
  private void showlistdia()
  {
    final string[] mlist={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
    alertdialog.builder listdia=new alertdialog.builder(diaallactivity.this);
    listdia.settitle("列表对话框");
    listdia.setitems(mlist, new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        /*下标是从0开始的*/
        showclickmessage(mlist[which]);
      }
    });
    listdia.create().show();
  }
  /*单项选择对话框*/
  int yourchose=-1;
  private void showsinchosdia()
  {
    final string[] mlist={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
    yourchose=-1;
    alertdialog.builder sinchosdia=new alertdialog.builder(diaallactivity.this);
    sinchosdia.settitle("单项选择对话框");
    sinchosdia.setsinglechoiceitems(mlist, 0, new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        yourchose=which;
      }
    });
    sinchosdia.setpositivebutton("确定", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        if(yourchose!=-1)
        {
          showclickmessage(mlist[yourchose]);
        }
      }
    });
    sinchosdia.create().show();
  }
  arraylist<integer> mychose= new arraylist<integer>();
  private void showmultichosdia()
  {
    final string[] mlist={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
    final boolean mchosests[]={false,false,false,false,false,false,false};
    mychose.clear();
    alertdialog.builder multichosdia=new alertdialog.builder(diaallactivity.this);
    multichosdia.settitle("多项选择对话框");
    multichosdia.setmultichoiceitems(mlist, mchosests, new dialoginterface.onmultichoiceclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which, boolean ischecked) {
        // todo auto-generated method stub
        if(ischecked)
        {
          mychose.add(which);
        }
        else
        {
          mychose.remove(which);
        }
      }
    });
    multichosdia.setpositivebutton("确定", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        int size=mychose.size();
        string str="";
        for(int i=0;i<size;i++)
        {
          str+=mlist[mychose.get(i)];
        }
        showclickmessage(str);
      }
    });
    multichosdia.create().show();
  }
  //进度读取框需要模拟读取
  progressdialog mreadprocessdia=null;
  public final static int max_readprocess = 100;
  private void showreadprocess()
  {
    mreadprocessdia=new progressdialog(diaallactivity.this);
    mreadprocessdia.setprogress(0);
    mreadprocessdia.settitle("进度条窗口");
    mreadprocessdia.setprogressstyle(progressdialog.style_horizontal);
    mreadprocessdia.setmax(max_readprocess);
    mreadprocessdia.show();
    new thread(this).start();
  }
  //新开启一个线程,循环的累加,一直到100然后在停止
  @override
  public void run()
  {
    int progress= 0;
    while(progress < max_readprocess)
    {
      try {
        thread.sleep(100);
        progress++;
        mreadprocessdia.incrementprogressby(1);
      } catch (interruptedexception e) {
        // todo auto-generated catch block
        e.printstacktrace();
      }
    }
    //读取完了以后窗口自消失
    mreadprocessdia.cancel();
  }
  /*读取中的对话框*/
  private void showprocessdia()
  {
    progressdialog processdia= new progressdialog(diaallactivity.this);
    processdia.settitle("进度条框");
    processdia.setmessage("内容读取中...");
    processdia.setindeterminate(true);
    processdia.setcancelable(true);
    processdia.show();
  }
  /*自定义对话框*/
  private void showcustomdia()
  {
    alertdialog.builder customdia=new alertdialog.builder(diaallactivity.this);
    final view viewdia=layoutinflater.from(diaallactivity.this).inflate(r.layout.custom_dialog, null);
    customdia.settitle("自定义对话框");
    customdia.setview(viewdia);
    customdia.setpositivebutton("确定", new dialoginterface.onclicklistener() {
      @override
      public void onclick(dialoginterface dialog, int which) {
        // todo auto-generated method stub
        edittext diainput=(edittext) viewdia.findviewbyid(r.id.txt_cusdiainput);
        showclickmessage(diainput.gettext().tostring());
      }
    });
    customdia.create().show();
  }
  /*popup window 来实现*/
  private void showcuspopup(view parent)
  {
    if(window == null)
    {
      popupview=layoutinflater.from(diaallactivity.this).inflate(r.layout.dia_cuspopup_dia, null);
      cuspopupbtn1=(button)popupview.findviewbyid(r.id.diacuspopupsure);
      window =new popupwindow(popupview,layoutparams.fill_parent,layoutparams.fill_parent);
    }
    window.setanimationstyle(r.style.popupanimation);
    /*必须调用setbackgrounddrawable, 因为popupwindow在初始时,会检测background是否为null,如果是ontouch or onkey events就不会相应,所以必须设置background*/
    /*网上也有很多人说,弹出pop之后,不响应键盘事件了,这个其实是焦点在pop里面的view去了。*/
    window.setfocusable(true);
    window.setbackgrounddrawable(new bitmapdrawable()); 
    window.update();
    window.showatlocation(parent, gravity.center_vertical, 0, 0);
    cuspopupbtn1.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        // todo auto-generated method stub
        showclickmessage("popup window的确定");
      }
    });
  }
  /*显示点击的内容*/
  private void showclickmessage(string message)
  {
    toast.maketext(diaallactivity.this, "你选择的是: "+message, toast.length_short).show();
  }
}

布局,就是一堆的button

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <textview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="各种dialog合集" />
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="普通dialog"
    android:id="@+id/btn_dianormal"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="多按钮dialog"
    android:id="@+id/btn_diamulti"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="列表dialog"
    android:id="@+id/btn_dialist"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="单项选择dialog"
    android:id="@+id/btn_diasigchos"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="多项选择dialog"
    android:id="@+id/btn_diamultichos"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="进度条dialog"
    android:id="@+id/btn_diareadprocess"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="读取中dialog"
    android:id="@+id/btn_diaprocess"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="自定义dialog"
    android:id="@+id/btn_diacustom"/>
  <button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="popupwindow实现的dialog"
    android:id="@+id/btn_popupdia"/>
</linearlayout>

以上是android开发必知 九种对话框的实现方法,代码主要用alertdialog生成的对话框,它的功能丰富、使用最广。