Andriod从入门到熟悉之 Dialog
程序员文章站
2022-03-10 15:47:22
...
公共部分
private AlertDialog alert = null;
private AlertDialog.Builder builder = null;
alert = null;
builder = new AlertDialog.Builder(MainAdpterActivity.this);
//含三个按钮的Dialog
alert = builder.setIcon(R.mipmap.group4)
.setTitle("股票大跌怎么办")
.setMessage("请参考拔网线立即止跌法")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainAdpterActivity.this,"点击了取消",Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainAdpterActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("其他", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainAdpterActivity.this,"其他",Toast.LENGTH_SHORT).show();
}
})
.create();
alert.show();
//普通列表Dialog
final String[] Person = new String[]{"恩来","少奇","泽东","德怀","小雅","丹阳"}; alert = builder.setIcon(R.mipmap.group4) .setTitle("选择你喜欢的人") .setItems(Person, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainAdpterActivity.this,"你选择了"+Person[which],Toast.LENGTH_SHORT).show(); } }) .create(); alert.show();
//单选列表对话框
alert = builder.setIcon(R.mipmap.group4)
.setTitle("选择你喜欢的人,智能选择一个")
.setSingleChoiceItems(Person, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainAdpterActivity.this,"你选择了"+Person[which],Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
})
.create();
alert.show();
final String[] Person = new String[]{"恩来","少奇","泽东","德怀","小雅"};
//记录列表状态数组
personLike = new boolean[]{false,false,false,false,false};
//多选列表对话框
alert = builder.setIcon(R.mipmap.group4)
.setTitle("选择你喜欢的人,智能选择一个")
.setMultiChoiceItems(Person, personLike, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
personLike[which] = isChecked;
}
})
.setNegativeButton("取消",null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String reslut = "";
for (int i =0;i<personLike.length;i++){
if (personLike[i]){
reslut += Person[i]+" ";
}
}
Toast.makeText(MainAdpterActivity.this,reslut,Toast.LENGTH_SHORT).show();
}
})
.create();
alert.show();
//自定义View Build.setView()
final LayoutInflater inflater = MainAdpterActivity.this.getLayoutInflater(); dialogView = inflater.inflate(R.layout.view_dialog_layout,null); builder.setView(dialogView); alert = builder.create(); alert.show(); dialogView.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alert.dismiss(); } });
上一篇: 计算器
推荐阅读
-
PHP之文件 php7 php环境搭建 php从入门到精通
-
MyBatis从入门到精通(八):MyBatis动态Sql之foreach标签的用法
-
MyBatis从入门到精通(四):MyBatis XML方式的基本用法之增删改
-
Linux命令(shell)从入门到精通 学习笔记之1 文件安全与权限
-
MyBatis从入门到精通(七):MyBatis动态Sql之choose,where,set标签的用法
-
Linux命令(shell)从入门到精通 学习笔记之2 使用find和xargs
-
学习笔记之Qt从入门到精通(三)
-
学习笔记之Qt从入门到精通(一)
-
HTML5之全局属性 (声明:内容节选自《HTML 5从入门到精通》)
-
软文营销从入门到精通之网络营销的优势