1、新建工程:DialogTest

2、编写布局文件:

(1)、main.xml 代码如下:

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <Button
13 android:id="@+id/button01"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="@string/button01"
17 />
18 <Button
19 android:id="@+id/button02"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:text="@string/button02"
23 />
24 <Button
25 android:id="@+id/button03"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:text="@string/button03"
29 />
30 <Button
31 android:id="@+id/button04"
32 android:layout_width="fill_parent"
33 android:layout_height="wrap_content"
34 android:text="@string/button04"
35 />
36 </LinearLayout>
37
38 2)alert_dialog_text_entry.xml 此部分布局是实现可以用户交互的。
39
40 <?xml version="1.0" encoding="utf-8"?>
41 <LinearLayout
42 xmlns:android="http://schemas.android.com/apk/res/android"
43 android:orientation="vertical"
44 android:layout_width="fill_parent"
45 android:layout_height="fill_parent">
46
47 <TextView
48 android:id="@+id/username_view"
49 android:layout_width="wrap_content"
50 android:layout_height="wrap_content"
51 android:layout_marginLeft="20dip"
52 android:layout_marginRight="20dip"
53 android:text="用户名"
54 android:textAppearance="@android:style/TextAppearance.Medium"
55 />
56 <EditText
57 android:id="@+id/username_edit"
58 android:layout_width="fill_parent"
59 android:layout_height="wrap_content"
60 android:layout_marginLeft="20dip"
61 android:layout_marginRight="20dip"
62 android:capitalize="none"
63 android:textAppearance="@android:style/TextAppearance.Medium"
64 />
65 <TextView
66 android:id="@+id/password_view"
67 android:layout_width="wrap_content"
68 android:layout_height="wrap_content"
69 android:layout_marginLeft="20dip"
70 android:layout_marginRight="20dip"
71 android:text="密码"
72 android:textAppearance="@android:style/TextAppearance.Medium"
73 />
74 <EditText
75 android:id="@+id/password_edit"
76 android:layout_width="fill_parent"
77 android:layout_height="wrap_content"
78 android:layout_marginLeft="20dip"
79 android:layout_marginRight="20dip"
80 android:capitalize="none"
81 android:password="true"
82 android:textAppearance="@android:style/TextAppearance.Medium"
83 />
84 </LinearLayout>

3、编写资源文件:strings.xml

View Code
 1   <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string name="hello">对话框</string>
4 <string name="app_name">Android Dialog</string>
5 <string name="button01">两个button的对话框</string>
6 <string name="button02">三个button的对话框</string>
7 <string name="button03">可以进行输入的对话框</string>
8 <string name="button04">进度框</string>
9 <string name="alert_dialog_two_buttons_title">这是一个提示框,点击取消后可以返回。</string>
10 <string name="alert_dialog_ok">确定</string>
11 <string name="alert_dialog_cancle">取消</string>
12 <string name="alert_dialog_three_buttons_title">标题部分</string>
13 <string name="alert_dialog_three_buttons_msg">对于程序员或创业团队来说,还是有必要拥有一个属于自己的博客。
14 Wordpress 曾经让个人或企业搭建博客变得非常容易。但是我们觉得 Wordpress
15 还是有些重量级,所以选择了一个非常轻便的工具 toto,一段只有200多行代码的Ruby应用程序。</string>
16 <string name="alert_dialog_something">进入详细</string>
17 <string name="alert_diaog_text_entry">请输入</string>
18 </resources>

4、开发主逻辑代码:DialogTest.java

View Code
  1 package com.dialogTest;
2
3 import com.listeview.R;
4
5 import android.app.Activity;
6 import android.app.AlertDialog;
7 import android.app.DatePickerDialog;
8 import android.app.Dialog;
9 import android.app.ProgressDialog;
10 import android.app.TimePickerDialog;
11 import android.app.AlertDialog.Builder;
12 import android.content.Context;
13 import android.content.DialogInterface;
14 import android.os.Bundle;
15 import android.text.method.CharacterPickerDialog;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.widget.Button;
20
21 public class DialogTest extends Activity {
22 private static final int dialog4 = 4;
23 private static final int dialog3 = 3;
24 private static final int dialog2 = 2;
25 private static final int dialog1 = 1;
26 /** Called when the activity is first created. */
27 private Button button01;
28 private Button button02;
29 private Button button03;
30 private Button button04;
31
32 @Override
33 public void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35 setContentView(R.layout.main);
36
37 button01=(Button)findViewById(R.id.button01);
38 button02=(Button)findViewById(R.id.button02);
39 button03=(Button)findViewById(R.id.button03);
40 button04=(Button)findViewById(R.id.button04);
41
42 button01.setOnClickListener(new OnClickListener() {
43 @Override
44 public void onClick(View v) {
45 showDialog(dialog1);
46 }
47 });
48
49 button02.setOnClickListener(new OnClickListener() {
50 @Override
51 public void onClick(View v) {
52 // TODO Auto-generated method stub
53 showDialog(dialog2);
54 }
55 });
56
57 button03.setOnClickListener(new OnClickListener() {
58 @Override
59 public void onClick(View v) {
60 // TODO Auto-generated method stub
61 showDialog(dialog3);
62 }
63 });
64
65 button04.setOnClickListener(new OnClickListener() {
66 @Override
67 public void onClick(View v) {
68 // TODO Auto-generated method stub
69 showDialog(dialog4);
70 }
71 });
72
73 }
74 @Override
75 protected Dialog onCreateDialog(int id) {
76 // TODO Auto-generated method stub
77 switch(id){
78 case dialog1:
79 return buildDialog1(DialogTest.this);
80 case dialog2:
81 return buildDialog2(DialogTest.this);
82 case dialog3:
83 return buildDialog3(DialogTest.this);
84 case dialog4:
85 return buildDialog4(DialogTest.this);
86 }
87 return null;
88 }
89 private Dialog buildDialog4(Context context) {
90 ProgressDialog dialog=new ProgressDialog(context);
91 dialog.setTitle("正在下载歌曲");
92 dialog.setMessage("请稍候......");
93
94 /*TimePickerDialog dialog=new TimePickerDialog(context, 0, null, 0, 0, false);
95 dialog.setTitle("时钟");*/
96
97 /*DatePickerDialog dialog=new DatePickerDialog(context, 0, null, 0, 0, 0);
98 dialog.setTitle("日期");*/
99
100 return dialog;
101 }
102 private Dialog buildDialog3(Context context) {
103 LayoutInflater inflater=LayoutInflater.from(this);
104 final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);
105
106 AlertDialog.Builder builder=new AlertDialog.Builder(context);
107 builder.setTitle(R.string.alert_diaog_text_entry);
108
109 builder.setView(textEntryView); //关键
110 builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
111
112 @Override
113 public void onClick(DialogInterface dialog, int which) {
114 // TODO Auto-generated method stub
115 setTitle("单击对话框上的确定按钮");
116 }
117 });
118 builder.setNegativeButton(R.string.alert_dialog_cancle,new DialogInterface.OnClickListener() {
119
120 @Override
121 public void onClick(DialogInterface dialog, int which) {
122 // TODO Auto-generated method stub
123 setTitle("单击了对话框上的取消按钮");
124 }
125 });
126 return builder.create();
127 }
128
129
130 private Dialog buildDialog2(Context context) {
131 AlertDialog.Builder builder=new AlertDialog.Builder(context);
132 builder.setTitle(R.string.alert_dialog_three_buttons_title);
133 builder.setMessage(R.string.alert_dialog_three_buttons_msg);
134 builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
135 @Override
136 public void onClick(DialogInterface dialog, int which) {
137 // TODO Auto-generated method stub
138 setTitle("单击对话框上的确定按钮");
139 }
140 });
141 builder.setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
142 @Override
143 public void onClick(DialogInterface dialog, int which) {
144 // TODO Auto-generated method stub
145 setTitle("点击了对话框上的进入详细按钮");
146 }
147 });
148 builder.setNegativeButton(R.string.alert_dialog_cancle, new DialogInterface.OnClickListener() {
149
150 @Override
151 public void onClick(DialogInterface dialog, int which) {
152 // TODO Auto-generated method stub
153 setTitle("单击了对话框上的取消按钮");
154 }
155 });
156 return builder.create();
157 }
158
159 private Dialog buildDialog1(Context context) {
160 // TODO Auto-generated method stub
161 AlertDialog.Builder builder=new AlertDialog.Builder(context);
162 //builder.setIcon(R.drawable.icon);
163 builder.setTitle(R.string.alert_dialog_two_buttons_title);
164 builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
165 @Override
166 public void onClick(DialogInterface dialog, int which) {
167 // TODO Auto-generated method stub
168 setTitle("单击对话框上的确定按钮");
169 }
170 });
171 builder.setNegativeButton(R.string.alert_dialog_cancle, new DialogInterface.OnClickListener() {
172 @Override
173 public void onClick(DialogInterface dialog, int which) {
174 // TODO Auto-generated method stub
175 setTitle("单击了对话框上的取消按钮");
176 }
177 });
178 return builder.create();
179 }
180 }

5、运行效果显示:

主程序:

Dialog1:

Dialog2:

Dialog3:

Dialog4:

另注:如果需要在模拟器中卸载此程序:可用如下命令:adb uninstall com.dialogTest

更多相关文章

  1. android中自己画一个按钮,和自己设置一个框框
  2. Android(安卓)AlertDialog.Builder进入和退出动画效果设置
  3. Android(安卓)Studio常用对话框
  4. android中常用的弹出提示框
  5. android selector 的几种状态
  6. 面向 x86 的 Android* 入门指南
  7. Android(安卓)将软键盘的回车按钮改变成搜索按钮
  8. Android对话框
  9. android 点击webView中的按钮修改布局问题

随机推荐

  1. MySQL btree索引与hash索引区别
  2. mysql group by 对多个字段进行分组操作
  3. 基于JPQL实现纯SQL语句方法详解
  4. MySQL删除表的三种方式(小结)
  5. MySQL复制表的三种方式(小结)
  6. mysql声明游标的方法
  7. mysql中写判断语句的方法总结
  8. MySQL主从复制延迟原因以及解决方案
  9. MySQL8安装Installer版的图文教程
  10. Mysql如何在linux中实现定时备份