Dialog - 常用类型 & 自定义布局
来源:互联网 发布:皖南医学院校园网络 编辑:程序博客网 时间:2024/06/16 12:50
一、常用 Dialog 类型
依次介绍:通知、列表、单选、复选、进度对话框
通知对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.mipmap.ic_launcher).setTitle("通知对话框").setMessage("爱不爱我").setPositiveButton("爱", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, "爱你", Toast.LENGTH_SHORT).show(); }}).setNegativeButton("不爱", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, "恨你", Toast.LENGTH_SHORT).show(); }}).show();
列表对话框
final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 列表选项AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.mipmap.ic_launcher).setTitle("列表对话框").setItems(names, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, names[which], Toast.LENGTH_SHORT).show(); }}).show();
单选对话框
final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 单选选项final String[] name = new String[1]; // 记录选中项AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.mipmap.ic_launcher).setTitle("单选对话框").setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { name[0] = names[which]; Toast.makeText(AActivity.this, name[0], Toast.LENGTH_SHORT).show(); }}).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, ("确定" + name[0]), Toast.LENGTH_SHORT).show(); }}).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, "取消选择", Toast.LENGTH_SHORT).show(); }}).show();
复选对话框
final String[] names = { "zhangsan", "wangwu", "lisi", "zhaoliu" }; // 复选选项final List<String> nameChosed = new ArrayList<>(); // 记录选中项AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.mipmap.ic_launcher).setTitle("多选对话框").setMultiChoiceItems(names, new boolean[]{false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String name = names[which]; if (isChecked) { nameChosed.add(name); Toast.makeText(AActivity.this, name + "OK", Toast.LENGTH_SHORT).show(); } else { nameChosed.remove(name); Toast.makeText(AActivity.this, name + "NO", Toast.LENGTH_SHORT).show(); } }}).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, "nameChosed.size():" + nameChosed.size() + "", Toast.LENGTH_SHORT).show(); }}).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AActivity.this, "取消", Toast.LENGTH_SHORT).show(); }}).show();
进度对话框
final ProgressDialog progressDialog = new ProgressDialog(this);progressDialog.setIcon(R.mipmap.ic_launcher);progressDialog.setTitle("进度对话框");progressDialog.setMessage("正在下载啊...");progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);progressDialog.setMax(100);progressDialog.show();new Thread(new Runnable() { @Override public void run() { while (true) { SystemClock.sleep(100); progressDialog.incrementProgressBy(5); if (progressDialog.getProgress() == progressDialog.getMax()) { runOnUiThread(new Runnable() { // 更新 UI @Override public void run() { progressDialog.dismiss(); Toast.makeText(AActivity.this, "进度完成", Toast.LENGTH_SHORT).show(); } }); break; } } }}).start();
二、自定义 Dialog 的布局
效果
xml布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#10f0" android:gravity="center" android:padding="5dp" android:text="设置密码" android:textSize="20sp" /> <EditText android:id="@+id/et_pwd_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:padding="5dp" /> <EditText android:id="@+id/et_pwd_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请确认密码" android:padding="5dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp"> <Button android:id="@+id/bt_YES" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确认" /> <Button android:id="@+id/bt_NO" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout></LinearLayout>
Java代码
private EditText etPWD1, etPWD2;private String pwdStr1, pwdStr2;public void showDialog(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View dialogView = View.inflate(this, R.layout.dialog_set_pwd, null); dialog.setView(dialogView, 0, 0, 0, 0); // 去边距,兼容2.X etPWD1 = (EditText) dialogView.findViewById(R.id.et_pwd_1); // 记录两次密码 etPWD2 = (EditText) dialogView.findViewById(R.id.et_pwd_2); dialogView.findViewById(R.id.bt_YES).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pwdStr1 = etPWD1.getText().toString(); pwdStr2 = etPWD2.getText().toString(); if (TextUtils.isEmpty(pwdStr1) || TextUtils.isEmpty(pwdStr2)) { Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show(); } else { if (pwdStr1.equals(pwdStr2)) { Toast.makeText(AActivity.this, "密码设置成功", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } else { Toast.makeText(getApplicationContext(), "密码不一致", Toast.LENGTH_SHORT).show(); } } } }); dialogView.findViewById(R.id.bt_NO).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show();}
错误疑问请提 catface.wyh@gmail.com
0 0
- Dialog - 常用类型 & 自定义布局
- 笔记8 Dialog常见类型及常用属性 自定义dialog
- Dialog自定义布局
- 自定义dialog布局
- 自定义Dialog&actionbar布局
- 自定义布局的dialog
- Dialog自定义布局展示
- 自定义dialog布局样式
- Android 自定义Dialog(自定义主题、自定义布局
- 自定义dialog的布局样式
- 自定义dialog的布局样式
- 【android控件】dialog自定义布局
- Android自定义dialog的布局
- 自定义dialog的布局样式
- Android dialog 对话框自定义布局
- 自定义Dialog显示布局,位置
- Android自定义dialog弹出布局
- Dialog自定义布局的实现
- 分析bacnet协议栈中的Device Communication Control服务
- MultiImageSelector Github项目地址
- android条形码二维码
- Ubuntu中SVN客户端安装+使用
- oracle数据迁移--复制表--create table as select & select into & insert into
- Dialog - 常用类型 & 自定义布局
- 数据结构之数组和广义表
- npm install -g ionic 安装
- 根据两个经纬度计算距离
- c++中Const,Const函数,Const变量,函数后面的Const
- 读视频文件和运动物体检测cvCaptureFromCAM cvQueryFrame
- Unity中的场景切换
- android中关于布局
- 百练 oj 2754 八皇后问题