当前位置:网站首页>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>

边栏推荐
- Sator launched Web3 game "satorspace" and launched hoobi
- VSCode关于C语言的3个配置文件
- Solidity函数学习
- 本周小贴士131:特殊成员函数和`= default`
- LeetCode 515(C#)
- mysql使用笔记一
- SlashData开发者工具榜首等你而定!!!
- [fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
- 【饭谈】如何设计好一款测试平台?
- Flask搭建api服务
猜你喜欢

Sator a lancé le jeu web 3 "satorspace" et a lancé huobi

Sator launched Web3 game "satorspace" and launched hoobi

PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight

【网络攻防原理与技术】第1章:绪论

网络攻防复习篇

命令模式 - Unity

麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
![[image sensor] correlated double sampling CDs](/img/1c/3a641ad47ff91536db602dedc82705.png)
[image sensor] correlated double sampling CDs

【TPM2.0原理及应用指南】 5、7、8章

Seaborn data visualization
随机推荐
网络攻防复习篇
Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
Repair method of firewall system crash and file loss, material cost 0 yuan
本周小贴士#140:常量:安全习语
DNS 系列(一):为什么更新了 DNS 记录不生效?
鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
SIGGRAPH 2022最佳技术论文奖重磅出炉!北大陈宝权团队获荣誉提名
NeRF:DeepFake的最终替代者?
Notes on installing MySQL in centos7
[image sensor] correlated double sampling CDs
【信息安全法律法規】複習篇
Devops' operational and commercial benefits Guide
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
无法链接远程redis服务器(解决办法百分百)
Mrs offline data analysis: process OBS data through Flink job
第3章业务功能开发(用户访问项目)
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
centos7安装mysql笔记