Android对话框
来源:互联网 发布:大数据时代财务转型 编辑:程序博客网 时间:2024/06/08 18:32
对话框
2017/9/29 13:43:02
确定取消
效果
代码
//创建对话框 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();
多按钮
效果
代码
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();
列表框
效果
代码
//条目数组 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();
单选列表框
效果
代码
//条目数据 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();
进度条
效果
代码
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();
多选列表框
效果
代码
//条目数据 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();
自定义消息框
效果
代码
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>
读取进度框
效果
代码
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);progressDialog.setTitle("正在读取");progressDialog.setIndeterminate(true);progressDialog.setCancelable(true);progressDialog.show();
阅读全文
0 0
- Android对话框
- Android对话框
- android 对话框
- android 对话框
- Android对话框
- Android对话框
- Android对话框
- Android对话框
- android对话框
- Android对话框
- android对话框
- Android对话框
- Android对话框
- android对话框
- android对话框
- Android对话框
- android 对话框
- android 对话框
- ubuntu14设置在当前目录打开终端
- Android开发 React Native项目创建及环境搭建
- 在Mac上搭建SylixOS开发环境
- HDU
- git和svn的区别
- Android对话框
- angulajs简单增删改查2
- eclipse中怎么修改所复制的web项目的部署名字
- 最新版Ubuntu 17.10与Windows双系统安装、配置与美化教程(转载)
- angularJs球员信息表
- iOS UIDatePicker的使用详情
- 解决:tomcat6 多个web项目页面出现 多次重定向错误无法访问的问题
- Gradle transitive = true
- Java 7之多线程第1篇 – 基础API介绍