Android入门之AlertDialog用法实例分析

时间:2022-01-17 22:26:57

本文实例讲述的是alertdialog,这种对话框会经常遇到。alertdialog跟win32开发中的dialog不一样,alertdialog是非阻塞的,而阻塞的对话框用的是popupwindow。

先贴出该程序运行的截图:

Android入门之AlertDialog用法实例分析

main.xml的源码:

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 
<button android:id="@+id/button01" android:layout_height="wrap_content" android:text="非layout型对话框" android:layout_width="fill_parent"></button>
<button android:id="@+id/button02" android:layout_height="wrap_content" android:text="layout型对话框" android:layout_width="fill_parent"></button><view android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content"></view>
 
</linearlayout>

下图是非layout型对话框,直接使用alertdialog

Android入门之AlertDialog用法实例分析

下图是使用了layout的对话框,可以自定义控件,实现更复杂的对话框

Android入门之AlertDialog用法实例分析

dialoglayout.xml的源码:

?
1
2
3
4
5
6
7
8
9
<?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="wrap_content"
 android:orientation="vertical">
 <edittext android:layout_height="wrap_content"
 android:layout_width="fill_parent" android:layout_marginleft="20dip"
 android:layout_marginright="20dip" android:textappearance="?android:attr/textappearancemedium" android:id="@+id/edtinput"/>
</linearlayout>

程序源码:

?
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
package com.testalertdialog;
 
import android.app.activity;
import android.app.alertdialog;
import android.content.context;
import android.content.dialoginterface;
import android.os.bundle;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.popupwindow;
 
 
public class testalertdialog extends activity {
 button btnshowdialog;
 button btnshowdialog_layout;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    //定义按钮
    btnshowdialog=(button)this.findviewbyid(r.id.button01);
    btnshowdialog.setonclicklistener(new clickevent());
    btnshowdialog_layout=(button)this.findviewbyid(r.id.button02);
    btnshowdialog_layout.setonclicklistener(new clickevent());
  }
  
  
  //统一处理按键事件
  class clickevent implements onclicklistener{
 
   @override
   public void onclick(view v) {
   // todo auto-generated method stub
   if(v==btnshowdialog)
    showdialog(testalertdialog.this);
    
   else if(v==btnshowdialog_layout)
    showdialog_layout(testalertdialog.this);
   
   }
 
  }
 
  //显示基本的alertdialog
 private void showdialog(context context) {
 alertdialog.builder builder = new alertdialog.builder(context);
 builder.seticon(r.drawable.icon);
 builder.settitle("title");
 builder.setmessage("message");
 builder.setpositivebutton("button1",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button1");
   }
  });
 builder.setneutralbutton("button2",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button2");
   }
  });
 builder.setnegativebutton("button3",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button3");
   }
  });
 builder.show();
 }
 
 
  //显示基于layout的alertdialog
 private void showdialog_layout(context context) {
 layoutinflater inflater = layoutinflater.from(this);
 final view textentryview = inflater.inflate(
  r.layout.dialoglayout, null);
 final edittext edtinput=(edittext)textentryview.findviewbyid(r.id.edtinput);
 final alertdialog.builder builder = new alertdialog.builder(context);
 builder.setcancelable(false);
 builder.seticon(r.drawable.icon);
 builder.settitle("title");
 builder.setview(textentryview);
 builder.setpositivebutton("确认",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle(edtinput.gettext());
   }
  });
 builder.setnegativebutton("取消",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("");
   }
  });
 builder.show();
 }
}