Android_对话框
来源:互联网 发布:玫瑰花园摆放软件 编辑:程序博客网 时间:2024/06/03 23:31
效果
代码
//创建对话框 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_异常
- Selenium Java自动化测试学习知识积累-TestNG
- 微信红包新玩法:利是说语音口令红包
- bat
- git常用命令
- ListView加优化的多条目加载
- Android_对话框
- Codeforces E. Field of Wonders
- 自定义view 倒计时
- ARM中的D-TCM(转)
- ActiveAndroid 通过 Sqlcipher 加密数据库
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- Js控制URL
- ISIS路由器分类—Level-2 区域间路由
- 数据库——Oracle中创建数据库