服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发必知 九种对话框的实现方法

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

2021-03-29 14:27Android开发网 Android

App中少不了与用户交互的各种dialog,以此达到很好的用户体验,下面给大家介绍Android开发必知 九种对话框的实现方法,有需要的朋友可以参考下

在开发过程中,与用户交互式免不了会用到对话框以实现更好的用户体验,所以掌握几种对话框的实现方法还是非常有必要的。在看具体实例之前先对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生成的对话框,它的功能丰富、使用最广。

延伸 · 阅读

精彩推荐
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4662021-01-03
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7442021-03-11
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4632021-03-09
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02