android 之 Dialog
Android平台下对话框主要有普通对话框、选项对话框、单选多选对话框、进度对话框、日期对话框、时间对话框等。
在程序中通过开发回调方法onCreateDialog来完成对话框的创建,该方法需要传入代表对话框id参数。如果要显示对话框,则调用showDialog方法传入对话框的id来显示指定对话框。
当对话框第一次被显示时,会调用onCreateDialog方法来创建对话框实例,之后将不再重复创建该实例。每次对话框在被显示之前都会调用onPrepareDialog方法,如不重写该方法,每次显示的对话框都将是最初创建的那个。
关闭对话框可用dismiss方法实现。但如果需要让对话框在关闭之后彻底被清除,要调用removeDialog方法并传入dialog的id来彻底释放对话框。
onDismissListener方法可在关闭对话框时执行一些特定工作。
例子如下:
界面上放置四个按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tv01" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/bt01" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="普通对话框" />
<Button android:id="@+id/bt02" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="列表对话框" />
<Button android:id="@+id/bt03" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="单选按钮对话框" />
<Button android:id="@+id/bt04" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="复选按钮对话框" />
<Button android:id="@+id/bt05" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="日期对话框" />
<Button android:id="@+id/bt06" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="时间对话框" />
<Button android:id="@+id/bt07" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="进度条对话框" />
<DigitalClock android:text="DigitalClock"
android:textSize="20dip" android:gravity="center" android:id="@+id/dclock01"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<AnalogClock android:text="AnalogClock" android:textSize="20dip"
android:gravity="center" android:id="@+id/aclock01"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
为每个按钮绑定OnClikListener,其中用showDialog(id)方法显示对话框:
bt01.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(COMMON_DIALOG);
}});
覆写onCreateDialog方法:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
Dialog dialog = null;
switch (id) {
case COMMON_DIALOG:
Builder b1 = new AlertDialog.Builder(this);
b1.setIcon(R.drawable.img01);
b1.setTitle("普通对话框标题");
b1.setMessage("普通对话框内容");
b1.setPositiveButton("确定", new OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
tv.setText("您选择了普通对话框");
}
});
dialog = b1.create();
break;
case LIST_DIALOG:
Builder b2 = new AlertDialog.Builder(this);
b2.setIcon(R.drawable.img01);
b2.setTitle("列表对话框标题");
b2.setItems(R.array.msa, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
tv.setText("您选择了:"+getResources().getStringArray(R.array.msa)[which]);
}
});
dialog = b2.create();
break;/*其中R.array.msa在res/values下array.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="msa">
<item>游泳</item>
<item>篮球</item>
<item>足球</item>
</string-array>
</resources>*/
case LIST_DIALOG_SINGLE:
Builder b3 = new AlertDialog.Builder(this);
b3.setIcon(R.drawable.img01);
b3.setTitle("单选按钮对话框标题");
b3.setSingleChoiceItems(R.array.msa, 0,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
tv.setText("您选择了:"+getResources().getStringArray(R.array.msa)[which]);
}
});
b3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = b3.create();
break;
case LIST_DIALOG_MULTIPLE:
Builder b4 = new AlertDialog.Builder(this);
b4.setIcon(R.drawable.img01);
b4.setTitle("复选按钮对话框标题");
b4.setMultiChoiceItems(R.array.msa, mulFlags,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
mulFlags[which]=isChecked;
String result="您选择了:";
for(int i=0;i<mulFlags.length;i++){
if(mulFlags[i]){
result+=items[i]+"、";
}
}
tv.setText(result.substring(0, result.length()-1));
}
});
b4.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = b4.create();
break;case DATE_DIALOG:
c=Calendar.getInstance();
dialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
tv.setText("您选择了"+year+"年"+monthOfYear+"月"+dayOfMonth+"日");
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG:
c=Calendar.getInstance();
dialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tv.setText("您选择了"+hourOfDay+"时"+minute+"分");
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),false);case PROGRESS_DIALOG:
pd = new ProgressDialog(this);
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("安装进度");
pd.setCancelable(false);
dialog=pd;
break;
default:
break;
}
return dialog;
}
显示进度进度条需要时刻更新进度条进度,因此需要覆写onPrepareDialpg方法:
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
switch (id) {
case PROGRESS_DIALOG:
pd.incrementProgressBy(-pd.getProgress());
new Thread() {//该线程负责进度对话框的进度,并发送消息至主Activity的handler对象进行更新。
public void run() {
while (true) {
myHandler.sendEmptyMessage(INCREASE);
if (pd.getProgress() >= 100) {
break;
}
try {
Thread.sleep(40);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
主函数中myhandler:
myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stubswitch (msg.what) {
case INCREASE:
pd.incrementProgressBy(1);
if (pd.getProgress() >= 100) {
pd.dismiss();
}
break;
}
super.handleMessage(msg);
}};
转载于:https://www.cnblogs.com/yechanglv/p/6923042.html
android 之 Dialog相关推荐
- 你真的会用Android的Dialog吗?由一个Exception想到的
一个Bug 前几日出现这样一个Bug是一个RuntimeException,详细信息是这样子的: java.lang.IllegalArgumentException: View not attach ...
- Android studio Dialog 弹出式对话框
文件结构 这里写图片描述 运行效果 这里写图片描述 主要代码 MainActivity package cn.edu.sicnu.dialogdemo; import android.app.Dial ...
- android 继承dialog自定义对话框
android 继承dialog自定义对话框 样式如下: 代码实现: 主要Activity: public class MainActivity extends Activity { Button ...
- Android自定义Dialog及与Activity的交互
一个小项目,需要用到一个自定义的Dialog,Dialog中需要一个ImageView用于显示表情图片,一个TextView用来显示提示文本,点击任何地方关闭Dialog,同时关闭打开这个Dialog ...
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户 ...
- Android 自定义dialog
my_dialog.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- android网络转圈,android基于dialog加载时转圈圈很好的demo
[实例简介] 这是一个android基于dialog加载时转圈圈很好的完整demo,很适合新手学习,希望对有需要的朋友能得到帮助 [实例截图] [核心代码] dialog_anim └── dialo ...
- android 自定义透明 等待 dialog,Android自定义Dialog内部透明、外部遮罩效果
Android自定义Dialog内部透明.外部遮罩效果 发布时间:2020-09-09 03:01:41 来源:脚本之家 阅读:117 作者:zst1303939801 本文实例为大家分享了Andro ...
- android 获取对话框对象,Android 基本Dialog和自定义Dialog
Android 基本Dialog和自定义Dialog Dialog类是对话框的基类,但你应该避免直接实例化Dialog ,可以使用子类 1.AlertDialog 此对话框可以显示标题,最多三个按钮, ...
- Android的Dialog类设计的太糟糕了!
题目的噱头是大了点. 但是,这是不争的事实. 我们可以看看这么一个需求:我要在一个界面上显示一个对话框.不管是简单的确认对话框,还是可以选择是否的对话框. Windows下面的代码: C# priva ...
最新文章
- 编译linux内核分区,Ubuntu编译内核及grub的一些笔记
- avro文件导入到hive_XML到Avro的转换
- android thread实例
- 吴恩达机器学习:偏差与方差、欠拟合与过拟合
- 【Arduino】开发入门【八】舵机操作+源代码
- [ NOI 2001 ] 食物链
- hsql mybatis 表不存在_Mybatis-plus 查询数据库表时抛异常提示数据库表不存在
- dc dc变换器的建模及matlab仿真,巧用Matlab仿真DC—DC变换器
- 必看!!PMP考试答题技巧
- WPS简历模板的图标怎么修改_官方发福利一起来薅羊毛啦!教你免费领WPS会员
- Minor, cofactor and adjoint matrix
- [读书笔记]多线程学习笔记
- Android Studio 打开Profiler后App闪退
- oracle 000000000A2F6630,数据导入新库时发生错误
- 网易考拉Android客户端路由总线设计 1
- mac一直ReportCrash
- android工具类怎么写,用kotlin写了一些android常用的一些工具类
- 红旗linux认证管理专家,linux认证:亲自感受红旗linux系统优化大全
- 记一次HDFS空间清理:Non DFS Used
- 【分布式任务调度】(一)XXL-JOB调度中心集群部署配置
热门文章
- 微信小程序——通过百度API查询天气情况
- 时间操作(JavaScript版)—页面显示格式:年月日星期几
- 【IDEA】报错:Warning:java: 源值1.5已过时, 将在未来所有发行版中删除
- SqlServer中Group By高级使用--Inner Join分组统计
- SqlServer修改sa的密码
- 去除DataTable重得记录(net 2.0,不支持Linq)
- LitePal(版本1.5.0,写此博客时是最新版本)
- 二级公共基础知识_二级公共基础知识 01
- (每日一题 day 003 - 二维前缀和+动态规划) 1314. 矩阵区域和
- 高级python_高级Python功能