android dialog
1.写在前面
- Android提供了丰富的
Dialog
函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式,将在第2部分介绍。 - 有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要
复写
Dialog的create()
、show()
、dismiss()
等方法,将在第3部分介绍。
2.代码示例
2.1 普通Dialog(图1与图2)
- 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
|
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
}
private void showNormalDialog(){
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity. this );
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle( "我是一个普通Dialog" )
normalDialog.setMessage( "你要点击哪一个按钮呢?" );
normalDialog.setPositiveButton( "确定" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton( "关闭" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 显示
normalDialog.show();
}
}
|
- 3个按钮
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
|
/* @setNeutralButton 设置中间的按钮
* 若只需一个按钮,仅设置 setPositiveButton 即可
*/
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity. this );
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle( "我是一个普通Dialog" ).setMessage( "你要点击哪一个按钮呢?" );
normalDialog.setPositiveButton( "按钮1" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNeutralButton( "按钮2" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNegativeButton( "按钮3" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
// 创建实例并显示
normalDialog.show();
}
|
2.2 列表Dialog(图3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private void showListDialog() {
final String[] items = { "我是1" , "我是2" , "我是3" , "我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity. this );
listDialog.setTitle( "我是一个列表Dialog" );
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which 下标从0开始
// ...To-do
Toast.makeText(MainActivity. this ,
"你点击了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
|
2.3 单选Dialog(图4)
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
|
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1" , "我是2" , "我是3" , "我是4" };
yourChoice = - 1 ;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity. this );
singleChoiceDialog.setTitle( "我是一个单选Dialog" );
// 第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items, 0 ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton( "确定" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != - 1 ) {
Toast.makeText(MainActivity. this ,
"你选择了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
|
2.4 多选Dialog(图5)
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
|
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1" , "我是2" , "我是3" , "我是4" };
// 设置默认选中的选项,全为false默认均未选中
final boolean initChoiceSets[]={ false , false , false , false };
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity. this );
multiChoiceDialog.setTitle( "我是一个多选Dialog" );
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton( "确定" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "" ;
for ( int i = 0 ; i < size; i++) {
str += items[yourChoices.get(i)] + " " ;
}
Toast.makeText(MainActivity. this ,
"你选中了" + str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}
|
2.5 等待Dialog(图6)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private void showWaitingDialog() {
/* 等待Dialog具有屏蔽其他控件的交互能力
* @setCancelable 为使屏幕不可点击,设置为不可取消(false)
* 下载等事件完成后,主动调用函数关闭该Dialog
*/
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity. this );
waitingDialog.setTitle( "我是一个等待Dialog" );
waitingDialog.setMessage( "等待中..." );
waitingDialog.setIndeterminate( true );
waitingDialog.setCancelable( false );
waitingDialog.show();
}
|
2.6 进度条Dialog(图7)
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
|
private void showProgressDialog() {
/* @setProgress 设置初始进度
* @setProgressStyle 设置样式(水平进度条)
* @setMax 设置进度最大值
*/
final int MAX_PROGRESS = 100 ;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity. this );
progressDialog.setProgress( 0 );
progressDialog.setTitle( "我是一个进度条Dialog" );
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加1
*/
new Thread( new Runnable() {
@Override
public void run() {
int progress= 0 ;
while (progress < MAX_PROGRESS){
try {
Thread.sleep( 100 );
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
}
}
// 进度达到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
}
|
2.7 编辑Dialog(图8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private void showInputDialog() {
/*@setView 装入一个EditView
*/
final EditText editText = new EditText(MainActivity. this );
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity. this );
inputDialog.setTitle( "我是一个输入Dialog" ).setView(editText);
inputDialog.setPositiveButton( "确定" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity. this ,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}
|
2.8 自定义Dialog(图9)
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
< EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</ 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
|
private void showCustomizeDialog() {
/* @setView 装入自定义View ==> R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
* dialog_customize.xml可自定义更复杂的View
*/
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity. this );
final View dialogView = LayoutInflater.from(MainActivity. this )
.inflate(R.layout.dialog_customize, null );
customizeDialog.setTitle( "我是一个自定义Dialog" );
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton( "确定" ,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取EditView中的输入内容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity. this ,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}
|
3.复写回调函数
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
|
/* 复写Builder的create和show函数,可以在Dialog显示前实现必要设置
* 例如初始化列表、默认选项等
* @create 第一次创建时调用
* @show 每次显示时调用
*/
private void showListDialog() {
final String[] items = { "我是1" , "我是2" , "我是3" , "我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity. this ){
@Override
public AlertDialog create() {
items[ 0 ] = "我是No.1" ;
return super .create();
}
@Override
public AlertDialog show() {
items[ 1 ] = "我是No.2" ;
return super .show();
}
};
listDialog.setTitle( "我是一个列表Dialog" );
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
/* @setOnDismissListener Dialog销毁时调用
* @setOnCancelListener Dialog关闭时调用
*/
listDialog.setOnDismissListener( new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
"Dialog被销毁了" ,
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
|
转载于:https://www.cnblogs.com/suibingbing/p/8193559.html
android dialog相关推荐
- Android --- Dialog 中 Dismiss 和 Cancel 的解释
Android Dialog有两个很相似的方法: Dialog.cancel()和Dialog.dismiss(); 这两个方法区别在于: 1.Dialog.cancel()方法中会调用Dialog. ...
- 7种形式的Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- android dialog隐藏虚拟按键,Android应用开发之Android dialog 去除虚拟按键的解决方法...
本文将带你了解Android应用开发Android dialog 去除虚拟按键的解决方法,希望本文对大家学Android有所帮助. Android 部分机型没有物理按键,只有虚拟按键,Activi ...
- android dialog去掉标题栏和边框
android Dialog去掉标题栏 和边框 首先在 values/Style.xml文件中加入以下代码(如果没有该文件就创建一个XML名为Style.xml) <!--重写系统弹出Dialo ...
- android的自定义dialog样式,如何自定义Android Dialog的样式?
如何自定义Android Dialog的样式?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 如何自定义Android ...
- android dialog 详解,详解android dialog自定义实例
现在很多应用程序的提示对话框都很有个性的,如果你使用的对话框样子还是传统的对话框,是不是有点LOST了呢?今天爱站技术频道小编就给大家讲讲详解android dialog自定义实例. 第一个需求:简单 ...
- Android Dialog总结
#.概述 Android系统提供的所有对话框类都继承自Dialog类,一般用到的是AlertDialog. 系统本身提供了AlertDialog的一些样式.也提供了AlertDialog的一些子类Di ...
- Android : Dialog中VideoView全屏播放
Android : Dialog中VideoView全屏播放 前几天公司要求在引导页播放视频,如果跳转到activity去进行播放,再返回时生命周期方法调动会不太好,最后改成了在引导页构建一个Dial ...
- android dialog横屏,解决dialog在横竖屏切换时消失
以AlertDialog为例 一.将AlertDialog视图封装在dialogfragment(fragmentdeAlertDialog子类)实例中 package com.example.t; ...
最新文章
- aaynctask控制多个下载进度_AsyncTask用法解析-下载文件动态更新进度条
- 在神经网络中使用dropout
- Java敌人跟地图的碰撞_(译)加入敌人和战斗:如果使用cocos2d制作基于tiled地图的游戏:第三部分...
- 上班族如何当老板 五大模式任你选
- win10安装misql8_Windows10下安装MySQL8.0
- ArcGIS学习总结(七)——河流制图综合
- 目标检测的数据增强 -- 代码实现
- 修改整个app的字体
- Unity资源加载简析(一)Resources
- switch日版有中文吗_Switch中文系统终于到来!近日将推送更新,美版日版港版均支持...
- 讲讲Python中的函数传递问题 【文末送书】
- 数字孪生核电站促进界面监测的应用实践
- Ubuntu18.04安装后要做的事
- 吉德林法则 (Kidlin's Law)的真实案例(2)
- Zprotect patch KEY 脱壳
- 真假李逵!你不是CHATGPT吗?
- 杭电 Problem 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
- 博思得条码打印机打印空白页处理方法
- 大数据在发电厂的应用_首例违反生活垃圾焚烧发电厂自动监测数据应用管理规定的违法企业被查处...
- 智云通CRM:如何用行动促进成交?(一)
热门文章
- 二叉树的高度-递归-JS实现
- 一个简单好用的Android Tab 设计与实现
- SQL 2014 in-memory中的storage部分
- WTL中CMemDC与CDC、CDCHandle的联系
- python做ui自动化_[python]RobotFramework自定义库实现UI自动化
- deepin 远程linux,在Deepin Linux操作系统中如何连接Microsoft OneDrive
- php 降低图像大小,尝试在php中调整图像大小时如何处理此内存泄漏?
- 让 typora和word一样好用
- ipython/jupyter notebook修改文件存储路径和浏览器
- 人工智能写散文之躲进你的心里记录温暖的你