当前位置:网站首页>alertDialog創建对话框
alertDialog創建对话框
2022-07-07 15:40:00 【XLMN】
alertDialog創建对话框
alertDialog生成对话框可以分为以下4个区域
1、图标区
2、标题区
3、内容区
4、按钮区
public class MainActivity extends Activity {
private ClipboardManager show;
String[] items = new String[]{
“列表项1”,
“列表项2”,
“列表项3”,
“列表项4”
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog);
}
public void simple(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("简单对话框")
//设置图标
.setIcon(R.drawable.mia3)
.setMessage("对话框第二行测试内容/n");
//为alterDalog.builder添加“确定”按钮
setPositiveButton(builder);
//为alterDalog.builer添加取消按钮
setNegativeButton(builder).create().show();
}
private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
//调用setpostivitybutton方法添加确定按钮
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了【确定】按钮");
}
});
}
private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了【取消】按钮");
}
});
}
public void simpleList(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("简单列表项对话框")
//设置图标
.setIcon(R.drawable.mia3)
//设置简单的列表项内容
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《" + items[which] + "》");
}
});
//为alertdialog.builder添加“确定”按钮
setPositiveButton(builder);
//为alertdialog.builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void singleChoice(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("单选列表项对话框")
//设置图标
.setIcon(R.drawable.mia3)
//设置单选列表项,默认选中第二项(索引为1)
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《" + items[which] + "》");
}
});
//为AlertDialog.builder 添加“确定”按钮
setPositiveButton(builder);
//为AlertDialog.builder添加“取消”按钮
setNegativeButton(builder)
.create().show();
}
public void multiChoice(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("多选列表项对话框")
//设置图标
.setIcon(R.drawable.mia3)
//设置多选列表项,设置勾选第二项,第四项
.setMultiChoiceItems(items, new boolean[]{false, true, false, true}, null);
//为alertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
//为alertDialog.builder添加“取消”按钮
setNegativeButton(builder)
.create().show();
}
public void customList(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("自定义列表对话框")
//设置图标
.setIcon(R.drawable.mia3)
//设置自定义列表项
.setAdapter(new ArrayAdapter<String>(this, R.layout.array_item, items), null);
//为alertdialog.builder添加“确定”按钮
setPositiveButton(builder);
//为alertdialog.builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void customView(View source) {
// 加载界面布局文件
TableLayout tl = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(this)
// 设置对话框图标
.setIcon(R.drawable.mia3)
// 设置对话框标题
.setTitle(“自定义对话框”)
// 设置对话框的view对象
.setView(tl)
// 为对话框设置一个确定按钮
.setPositiveButton(“登录”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//此处可执行登录处理
}
})
// 为对话框设置一个取消按钮
.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消登录不进行任何操作
}
})
//創建并显示对话框
.create().show();
}
}
<?xml version="1.0" encoding="utf-8"?><Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="customList"
android:text="自定义列表对话框"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="customView"
android:text="自定义view对话框"/>
<?xml version="1.0" encoding="utf-8"?> <TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="10pt" />
<!--输入用户名文本框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="10pt" />
<!--输入密码的文本框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写密码"
android:password="true" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码"
android:textSize="10pt" />
<!--输入电话号码的文本框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的密码"
android:selectAllOnFocus="true"
android:phoneNumber="true" />
</TableRow>
边栏推荐
- 本周小贴士#140:常量:安全习语
- How to add aplayer music player in blog
- Problems encountered in Jenkins' release of H5 developed by uniapp
- Sator launched Web3 game "satorspace" and launched hoobi
- Proxmox VE重装后,如何无损挂载原有的数据盘?
- 【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
- 如何在博客中添加Aplayer音乐播放器
- LeetCode刷题day49
- What is cloud computing?
- 责任链模式 - Unity
猜你喜欢
A tour of grpc:03 - proto serialization / deserialization
LeetCode刷题day49
【网络攻防原理与技术】第5章:拒绝服务攻击
Sator推出Web3游戏“Satorspace” ,并上线Huobi
【信息安全法律法規】複習篇
【可信计算】第十二次课:TPM授权与会话
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial
让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展
Linux 安装mysql8.X超详细图文教程
Shallow understanding Net core routing
随机推荐
【饭谈】如何设计好一款测试平台?
From Devops to mlops: how do it tools evolve to AI tools?
使用Stace排除故障的5种简单方法
Solidity函数学习
让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展
redis主从、哨兵主备切换搭建一步一步图解实现
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
如何在软件研发阶段落地安全实践
The computer cannot add a domain, and the Ping domain name is displayed as the public IP. What is the problem? How to solve it?
Enum + Validation 的个人最佳实践 demo 分享
Rpcms method of obtaining articles under the specified classification
第2章搭建CRM项目开发环境(搭建开发环境)
How to add aplayer music player in blog
Jenkins发布uniapp开发的H5遇到的问题
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
mysql使用笔记一
管理VDI的几个最佳实践
浅谈 Apache Doris FE 处理查询 SQL 源码解析
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
命令模式 - Unity