android支持丰富的对话框,常用4中对话框:
1.AlertDialog:
2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装
3.DatePickerDialog:日期选择对话框,这个对话框只是对DatePicker的包装
4.TimePickerDialog:时间选择对话框,这个对话框只是对TimePicker的包装
AlertDialog
==>
AlertDialog支持的4中预定义对话框:
1.带消息、带N个按钮的提示对话框
2.带消息、带N个按钮的列表对话框
3.带多个单选列表项,带N个按钮的对话框
4.带多个多选列表项,带N个按钮的对话框
AlertDialog,也可以创建界面自定义对话框,使用AlertDialog创建对话框的大致步骤如下:
1.创建AlertDialog.Builder对象——该对象是AlertDialog的创建器
2.调用AlertDialog.Builder的方法对话框设置图标、标题、内容等
3.调用AlertDialog.Builder的create()方法创建AlertDialog对话框
4.调用AlertDialog.Builder的Show()方法显示对话框
实例一
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <EditText android:id= "@+id/edit" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "test" /> </LinearLayout> 代码实现==》 package com.example.myalertdialog1; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.text.style.BulletSpan; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnTest = (Button) this .findViewById(R.id.btnTest); // final EditText edit = (EditText) findViewById(R.id.edit); // 定义一个AlertDialog对象 final Builder builder = new AlertDialog.Builder( this ); btnTest.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { builder.setIcon(R.drawable.one); builder.setTitle( "自定义普通对话框" ); builder.setMessage( "提示对话框" ); builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText edit = (EditText) findViewById(R.id.edit); edit.setText( "您单击了确定" ); } }); builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText edit = (EditText) findViewById(R.id.edit); edit.setText( "您单击了取消" ); } }); builder.create().show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
实现效果:
注意:
以上实例实现了设置对话框图标、标题、等属性,并为按钮添加了两个按钮,除此之外,AlertDialog.Builder还提供了如下方法添加按钮:
setNeutralButton(charSequence text,DialogInterface.OnClickListener listener)——添加一个装饰性按钮==》android的对话框一共可以生成三个对话框。
使用AlertDialog创建列表对话框
AlertDialog.Builder除了提供了setMessage()设置对话框所显示的消息之外,还提供了如下方法来设置对话框显示列表内容:
setItems(int itemsId,DialogInterface.OnClickListener listener):创建普通列表对话框;
setMultiChoiceItems(CharSquence[] items,boolean[] checkedItems,DialogInterface.OnMultiChoiceListener listener):创建多选列表对话框;
setSingleChoiceItems(CharSquence[] items,int checkedItem,DialogInterface.OnClickListener listener):创建单选按钮列表对话框;
setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener):创建根据ListAdapter提供列表项的列表对话框;
注意:AlertDialog.Builder除了提供以上方法,还提供了一些重载的方法,用于为对话框添加列表项。
实例二:通过setItems方法实现列表对话框
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <TextView android:id= "@+id/edit" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "test" /> </LinearLayout> 代码实现==》 package com.example.myalertdialog2; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) this .findViewById(R.id.btnTest); final TextView tv = (TextView) this .findViewById(R.id.edit); final Builder builder = new AlertDialog.Builder( this ); btn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { builder.setIcon(R.drawable.one); builder.setTitle( "简单列表对话框" ); builder.setItems( new String[] { "0001" , "0002" , "0003" }, new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: tv.setBackgroundColor(Color.RED); break ; case 1: tv.setBackgroundColor(Color.GREEN); break ; case 2: tv.setBackgroundColor(Color.BLUE); break ; } } }); builder.create().show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
实现效果:
注意:
如果调用AlertDialog.Builder是setSingleChoiceItems、setMultieChoiceItems、setAdapter方法设置列表项,则可创建单选列表对话框、多选列表对话框、自定义列表对话框。
实例三:使用AlertDialog创建单选按钮列表框
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <TextView android:id= "@+id/edit" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "test" /> </LinearLayout> 代码实现==》 package com.example.myalertdialog3; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private final int Sing_Dialog = 0x113; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) this .findViewById(R.id.btnTest); tv = (TextView) this .findViewById(R.id.edit); btn.setOnClickListener( new OnClickListener() { @SuppressWarnings( "deprecation" ) @Override public void onClick(View v) { showDialog(Sing_Dialog); } }); } // 重写onCreateDialog方法创建对话框 @Override protected Dialog onCreateDialog( int id, Bundle args) { switch (id) { case Sing_Dialog: Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.drawable.one); builder.setTitle( "简单列表对话框" ); // 1 代表默认选择第二项,索引从0开始 builder.setSingleChoiceItems( new String[] { "0001" , "0002" , "0003" }, 1, new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: tv.setBackgroundColor(Color.RED); break ; case 1: tv.setBackgroundColor(Color.GREEN); break ; case 2: tv.setBackgroundColor(Color.BLUE); break ; } } }); // 添加一个确定按钮——用于关闭对话框 builder.setPositiveButton( "确定" , null ); return builder.create(); } return null ; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
运行效果:
注意:
通过调用AlertDialog.Builder的setSingleChoiceItems方法即可创建一个单选列表对话框;
该实例采用基于Activity回调的方式开发对话框。
采用基于Activity回调的方式开发对话框操作步骤:
1.重写Activity的OnCreateDialog(),该方法返回一个对话框。该方法内部一样通过AlertDialog.Builder或DatePickerDialog等创建对话框并返回。
2.程序需要显示对话框时调用Activity的ShowDialog()即可。
实例四:使用AlertDialog创建多选列表对话框
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <EditText android:id= "@+id/edit" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "test" /> </LinearLayout> 代码实现==》 package com.example.myalertdialog4; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private final int Multi_Dialog = 0x113; private boolean[] CheckedStatus = new boolean[] { true , false , true }; private String[] Colors = new String[] { "0001" , "0002" , "0003" }; private EditText etTest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) this .findViewById(R.id.btnTest); etTest = (EditText) this .findViewById(R.id.edit); btn.setOnClickListener( new OnClickListener() { @SuppressWarnings( "deprecation" ) @Override public void onClick(View v) { showDialog(Multi_Dialog); } }); } // 重写onCreateDialog方法创建对话框 @Override protected Dialog onCreateDialog( int id, Bundle args) { switch (id) { case Multi_Dialog: Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.drawable.one); builder.setTitle( "多选列表对话框" ); builder.setMultiChoiceItems(Colors, CheckedStatus, new AlertDialog.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String tag= "您选择了:" ; for ( int i = 0; i < CheckedStatus.length; i++) { if (CheckedStatus[i]) { tag+=Colors[i]+ "、" ; } } etTest.setText(tag); } }); // 添加一个确定按钮——用于关闭对话框 builder.setPositiveButton( "确定" , null ); return builder.create(); } return null ; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
运行效果:
注意:
只需要调用AlertDialog.Builder的setMultieChoiceItems方法即可创建一个多选列表框的对话框。
调用AlertDialog.Builder的setMultieChoiceItems方法时,需要传人一个boolean[]参数,该参数的作用:
1.设置初始化时选中那些列表项;
2.boolean[]参数还可用于动态地获取多选列表中列表框的选中状态。
实例五:使用AlertDialog创建自定义对话框
注意:使用AlertDialog可以创建自定义对话框,Eg:调用AlertDialog.Builder的setAdapter方法来确定列表项,就可以生成自定义列表项的对话框。
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 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <EditText android:id= "@+id/edit" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "test" /> </LinearLayout> <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "horizontal" > <ImageView android:id= "@+id/header" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <TextView android:id= "@+id/name" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> </LinearLayout> 代码实现==》 package com.example.myalertdialog5; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private final int List_Dialog = 0x113; private String[] Names = new String[] { "大鸟" , "小鸟" , "老鸟" }; private int [] ImageIds = new int [] { R.drawable.ss, R.drawable.ele, R.drawable.sw }; private EditText etTest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) this .findViewById(R.id.btnTest); etTest = (EditText) this .findViewById(R.id.edit); btn.setOnClickListener( new OnClickListener() { @SuppressWarnings( "deprecation" ) @Override public void onClick(View v) { showDialog(List_Dialog); } }); } @Override protected Dialog onCreateDialog( int id, Bundle args) { switch (id) { case List_Dialog: Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.drawable.ele); builder.setTitle( "单选列表对话框" ); List<Map<String, Object>> map = new ArrayList<Map<String, Object>>(); for ( int i = 0; i < Names.length; i++) { Map<String, Object> item = new HashMap<String, Object>(); item.put( "header" , ImageIds[i]); item.put( "birdsnames" , Names[i]); map.add(item); } SimpleAdapter adapter = new SimpleAdapter( this , map, R.layout.row, new String[] { "birdsnames" , "header" }, new int [] { R.id.name, R.id.header }); builder.setAdapter(adapter, new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { etTest.setText( "你选择了喜欢:" + Names[which]); } }); return builder.create(); } return null ; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
注意:开发者也可以完全控制对话框内容,AlertDialog.Builder提供了一个setView(View view)——该方法允许设置对话框显示的内容为View组件(此处的组件可以是一个布局容器)。
运行效果:
Activity以对话框方式显示
注意:需要设置AndroidMainfest.xml配置文件,如下:
1 2 3 4 5 6 7 8 9 10 | <activity android:name= "com.example.myalertdialog6.MainActivity" android:label= "Activity对话框方式显示" android:theme= "@android:style/Theme.Dialog" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> |
实例六:
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 | 布局文件==》 <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "horizontal" > <ImageView android:id= "@+id/header" android:background= "@drawable/eighteen" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnTest" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "关闭" /> </LinearLayout> AndroidMainfest.xml==> <?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "com.example.myalertdialog6" android:versionCode= "1" android:versionName= "1.0" > <uses-sdk android:minSdkVersion= "8" android:targetSdkVersion= "18" /> <application android:allowBackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/AppTheme" > <!-- acitvity对话框样式设置 android:theme= "@android:style/Theme.Dialog" --> <activity android:name= "com.example.myalertdialog6.MainActivity" android:label= "Activity对话框方式显示" android:theme= "@android:style/Theme.Dialog" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 代码实现==》 package com.example.myalertdialog6; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) this .findViewById(R.id.btnTest); btn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
运行效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本