我是靠谱客的博主 粗犷季节,这篇文章主要介绍Android AlertDialog的几种用法详解,现在分享给大家,希望可以做个参考。

AlertDialog的几种用法

xml代码:

复制代码
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
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单的dialog" android:onClick="dialog_1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表的dialog" android:onClick="dialog_2"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选的dialog" android:onClick="dialog_3"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选的dialog" android:onClick="dialog_4"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义View的dialog" android:onClick="dialog_5"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用adapter的dialog" android:onClick="dialog_6"/> </LinearLayout>

java代码:

复制代码
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
package com.example.lesson7_3_id19_alertdialog; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void dialog_1(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher_round); builder.setTitle("标题栏"); builder.setMessage("正文部分,简单的文本"); builder.setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.setNeutralButton("中立",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } private String [] item = {"游戏","运动","电影","旅游","看书"}; public void dialog_2(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择"); builder.setItems(item, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show(); } }); // 取消可以不添加 //builder.setNegativeButton("取消",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } int index; public void dialog_3(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择"); builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { index = which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } // 设置boolean数组所有的选项设置默认没选 boolean[] bools = {false,false,false,false,false}; public void dialog_4(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择"); builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { bools[which] = isChecked; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < item.length; i++) { if (bools[i]) { sb.append(item[i] + " "); } } Toast.makeText(MainActivity.this, "选择了" + sb.toString(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } public void dialog_5(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("连接wifi"); final EditText et = new EditText(this); et.setHint("请输入密码"); et.setSingleLine(true); builder.setView(et); builder.setNegativeButton("取消",null); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String password = et.getText().toString(); if (password.equals("123456")) { Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } public void dialog_6(View v){ ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("使用适配器"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }

 

到此这篇关于Android AlertDialog的几种用法详解的文章就介绍到这了,更多相关Android AlertDialog方法内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是粗犷季节最近收集整理的关于Android AlertDialog的几种用法详解的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部