Android对话框
2017-10-24 09:53
127 查看
对话框
2017/9/29 13:43:02确定取消
效果
![](https://i.imgur.com/wgRGCvL.gif)
代码
//创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设置图标 builder.setIcon(R.mipmap.ic_launcher); //设置标题 builder.setTitle("确定退出!"); //设置确定按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_LONG).show(); } }); //设置取消按钮 如果只是要取消对话框 点击事件可以传NULL builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_LONG).show(); } }); //显示对话框 builder.show();
多按钮
效果
![](https://i.imgur.com/ahLr02C.gif)
代码
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("提示"); builder.setMessage("是否保存文件?"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "是", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "否", Toast.LENGTH_LONG).show(); } }); builder.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show(); } }); builder.show();
列表框
效果
![](https://i.imgur.com/32cxBig.gif)
代码
//条目数组 final String[] arr = new String[]{ "香蕉", "苹果", "橘子", "西瓜", "葡萄" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择你喜欢的水果!"); //设置条目 builder.setItems(arr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show(); } }); //创建对话框并且显示 builder.create().show();
单选列表框
效果
![](https://i.imgur.com/thYjZo3.gif)
代码
//条目数据 final String[] arr = new String[]{ "香蕉", "苹果", "橘子", "西瓜", "葡萄" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择你喜欢的水果!"); //设置条目 builder.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); builder.setNeutralButton("取消", null); builder.create().show();
进度条
效果
![](https://i.imgur.com/Cd62hyn.gif)
代码
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); //设置标题 progressDialog.setTitle("正在下载..."); //设置风格 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置最大大进度 progressDialog.setMax(100); progressDialog.setButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); progressDialog.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); progressDialog.show(); //创建线程模拟下载进度 new Thread(new Runnable() { @Override public void run() { int i = 0; while (i <= 100) { i++; //设置进度 progressDialog.incrementProgressBy(i); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();
多选列表框
效果
![](https://i.imgur.com/NmiAjbI.gif)
代码
//条目数据 final String[] arr = new String[]{ "香蕉", "苹果", "橘子", "西瓜", "葡萄" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择你喜欢的水果!"); //设置条目 builder.setMultiChoiceItems(arr, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { if (b) { Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show(); } } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); builder.setNeutralButton("取消", null); builder.create().show();
自定义消息框
效果
![](https://i.imgur.com/l1AH8sA.gif)
代码
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view1 = View.inflate(MainActivity.this, R.layout.item, null); builder.setView(view1); EditText et_user = view1.findViewById(R.id.et_user); EditText et_pass = view1.findViewById(R.id.et_pass); Button btn_login = view1.findViewById(R.id.btn_login); Button btn_right = view1.findViewById(R.id.btn_right); final AlertDialog alertDialog = builder.create(); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show(); alertDialog.dismiss(); } }); btn_right.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_LONG).show(); alertDialog.dismiss(); } }); alertDialog.show();
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:layout_margin="10dp" android:orientation="vertical" android:padding="10dp"> <EditText android:hint="请输入用户名" android:id="@+id/et_user" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="请输入密码" android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="登陆" /> <Button android:id="@+id/btn_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="注册" /> </LinearLayout> </LinearLayout>
读取进度框
效果
![](https://i.imgur.com/XOVBRS8.gif)
代码
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("正在读取"); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show();
相关文章推荐
- android---朋友圈评论功能自定义对话框
- android 对话框的应用
- Android 对话框【Dialog】去除白色边框代码
- android Dialog的确定按钮点击后不取消对话框
- Android——点击对话框上button不关闭对话框
- android封装框架入门之从自定义对话框开始callback帮你忙
- Android使用Activity用作弹出式对话框
- Android关闭开机弹出SIM卡变动提示对话框
- Android UI设计--半透明效果对话框及activity(可做遮罩层)
- Android自定义alertDialog和对话框中的点击事件
- Android之AlertDialog自定义对话框
- android 对话框吐司
- [Android Pro] Android 官方推荐 : DialogFragment 创建对话框
- android 对话框
- android中的对话框
- android 对话框弹出位置和透明度的设置
- android 返回键退出程序时弹出对话框请求确认退出
- Android 透明的Dialog对话框
- Android:AlertDialog对话框
- 实现一个登录对话框 (Android开发)