android 8种对话框(Dialog)使用方法汇总
推荐阅读
TensorFlow 2.0 (九) - 强化学习 70行代码实战 Policy Gradient
TensorFlow 2.0 (八) - 强化学习 DQN 玩转 gym Mountain Car
TensorFlow 2.0 (七) - 强化学习 Q-Learning 玩转 OpenAI gym
TensorFlow 2.0 (六) - 监督学习玩转 OpenAI gym game
TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络)
TensorFlow入门(四) - mnist手写数字识别(制作h5py训练集)
TensorFlow入门(三) - mnist手写数字识别(可视化训练)
TensorFlow入门(二) - mnist手写数字识别(模型保存加载)
TensorFlow入门(一) - mnist手写数字识别(网络搭建)
1.写在前面
- Android提供了丰富的
Dialog
函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮)、列表、单选、多选、等待、进度条、编辑、自定义等多种形式,将在第2部分介绍。 - 有时,我们希望在对话框创建或关闭时完成一些特定的功能,这需要
复写
Dialog的create()
、show()
、dismiss()
等方法,将在第3部分介绍。
2.代码示例
2.1 普通Dialog(图1与图2)
- 2个按钮
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
}
private void showNormalDialog(){
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog")
normalDialog.setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 显示
normalDialog.show();
}
}
- 3个按钮
/* @setNeutralButton 设置中间的按钮
* 若只需一个按钮,仅设置 setPositiveButton 即可
*/
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("按钮1",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNeutralButton("按钮2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNegativeButton("按钮3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
// 创建实例并显示
normalDialog.show();
}
2.2 列表Dialog(图3)
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which 下标从0开始
// ...To-do
Toast.makeText(MainActivity.this,
"你点击了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
2.3 单选Dialog(图4)
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1","我是2","我是3","我是4" };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一个单选Dialog");
// 第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
"你选择了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
2.4 多选Dialog(图5)
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
// 设置默认选中的选项,全为false默认均未选中
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle("我是一个多选Dialog");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += items[yourChoices.get(i)] + " ";
}
Toast.makeText(MainActivity.this,
"你选中了" + str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}
2.5 等待Dialog(图6)
private void showWaitingDialog() {
/* 等待Dialog具有屏蔽其他控件的交互能力
* @setCancelable 为使屏幕不可点击,设置为不可取消(false)
* 下载等事件完成后,主动调用函数关闭该Dialog
*/
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("我是一个等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}
2.6 进度条Dialog(图7)
private void showProgressDialog() {
/* @setProgress 设置初始进度
* @setProgressStyle 设置样式(水平进度条)
* @setMax 设置进度最大值
*/
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("我是一个进度条Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加1
*/
new Thread(new Runnable() {
@Override
public void run() {
int progress= 0;
while (progress < MAX_PROGRESS){
try {
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
}
}
// 进度达到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
}
2.7 编辑Dialog(图8)
private void showInputDialog() {
/*@setView 装入一个EditView
*/
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle("我是一个输入Dialog").setView(editText);
inputDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}
2.8 自定义Dialog(图9)
<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
private void showCustomizeDialog() {
/* @setView 装入自定义View ==> R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
* dialog_customize.xml可自定义更复杂的View
*/
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this);
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle("我是一个自定义Dialog");
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取EditView中的输入内容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}
3.复写回调函数
/* 复写Builder的create和show函数,可以在Dialog显示前实现必要设置
* 例如初始化列表、默认选项等
* @create 第一次创建时调用
* @show 每次显示时调用
*/
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this){
@Override
public AlertDialog create() {
items[0] = "我是No.1";
return super.create();
}
@Override
public AlertDialog show() {
items[1] = "我是No.2";
return super.show();
}
};
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
/* @setOnDismissListener Dialog销毁时调用
* @setOnCancelListener Dialog关闭时调用
*/
listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
"Dialog被销毁了",
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
android 8种对话框(Dialog)使用方法汇总的更多相关文章
- 【Android】Android 8种对话框(Dialog)
1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...
- 【Android 复习】:Android五种布局的使用方法
---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局: FrameLayout 3) ...
- eclipse环境下无法创建android virtual Devices(AVD)问题解决的方法汇总
首先,要在eclipse环境下成功的创建一个安卓虚拟机,须要有三项东西,第一就是eclipse,第二就是android SDK Manager,第三就是ADT,也就是eclipse环境下的一个安卓虚拟 ...
- Android几种打开SQLite的方法
第一种:用SQLiteOpenHelper辅助类 SQLiteOpenHelper类可以用来创建或打开数据库,两个关键的方法:onCreate(SQLiteDatabase db)和onUpgrade ...
- Android 弹出对话框Dialog充满屏幕宽度
final View view = LayoutInflater.from(context).inflate(layoutId, null); final Dialog dialog = new Di ...
- Android学习之六种事件响应方法汇总
java源码如下: 1.MainActivity.java源码 package com.example.responsetest; import android.app.Activity; impor ...
- android四种更新UI的方法
笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- 四种对话框(dialog)的简单使用方法
有普通对话框,单选对话框,复选对话框,进度条的两种实现方法话不多说,直接上代码 activity_main.xml: <?xml version="1.0" encoding ...
随机推荐
- C语言文件方式输入与输出(最简洁方便实用的一种方式)
freopen("inputfile.txt", "r", stdin); freopen("outputfile.txt", " ...
- 安卓actionbar源码
安卓actionbar源码,该源码转载源码天堂android源码频道的,Actionbar是一个标识应用程序和用户位置的窗口功能.源码我也上传到源码天堂了,大家也可以去那边下载就行了. 本地:源码源码 ...
- 基于类型系统的面向对象编程语言Go
(整理自网络) 面向对象编程 Go语言的面向对象编程(OOP)非常简洁而优雅.说它简洁,在于它没有了OOP中很多概念,比如:继承.虚函数.构造函数和析构函数.隐藏的this指针等等.说它优雅,是它的面 ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
- 《Java4Android》视频学习笔记——为什么用抽象类?
我们来举个例子,然后引出这个问题的答案: 市面上有 喷墨式打印机 和 针式打印机 这两种形式的打印机,我们需要编程来实现他们的 开机,关机以及打印. 构建父类Printer class Printer ...
- Android提升篇系列:Android项目代码优化实践
Android开发中,不同的开发团队,不同的开发人员,在实际编码中会有一些不同的地方.但是,具有一定的更普适性的编码习惯,无疑还是相当重要的.本文主要罗列项目中常见的一些编码片段,并给出相关建议. 1 ...
- sql2000安装的一般问题
SQLServer2000 在一段时间不使用后突然间不能够运行了.只能打开企业管理器,对数据库进行操作.VS2005不能够连接,试了很多种方式,无结果.于是重新安装 sqlServer2000? 仿真 ...
- Xdebug文档(一)基本特性
基本属性(参数) xdebug.default_enable 类型: boolean,默认值: 1 这是xdebug的基本设置,默认在调试跟踪时显示错误信息.可以使用xdebug_disable()函 ...
- Validating HTTP data with Play
Validations ensure that the data has certain values or meets specific requirements. You can use vali ...
- 决战大数据之二:CentOS 7 最新JDK 8安装
决战大数据之二:CentOS 7 最新JDK 8安装 [TOC] 修改hostname # hostnamectl set-hostname node1 --static # reboot now 重 ...