Android_文档学习_UI_Creating Dialogs

这篇博客详细介绍了在Android中如何创建各种Dialog,包括AlertDialog、ProgressDialog、DatePickerDialog、TimePickerDialog和自定义Dialog。通过示例代码展示了如何添加按钮、列表、复选框和单选按钮,以及如何展示进度条。提供了完整的菜单选项和事件处理方法,帮助开发者更好地理解和应用Dialog。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Creating Dialogs

Android中的Dialog,(既对话框).     包括1)AlertDialog 2) ProgressDialog 3) DatePickerDialog 4) TimePickerDialog 5) Custom Dialog

本文测试代码csdn下载频道:http://download.youkuaiyun.com/source/2903639

我发现要将文档理顺,其实最好就是全文翻译,但我不想这么做,我就把大致的理一下.方便自己和别人以后查阅使用.

AlertDialog
A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable items that can include checkboxes or radio buttons. The AlertDialog is capable of constructing most dialog user interfaces and is the suggested dialog type. See Creating an AlertDialog below.
ProgressDialog
A dialog that displays a progress wheel or progress bar. Because it's an extension of the AlertDialog, it also supports buttons. See Creating a ProgressDialog below.
DatePickerDialog
A dialog that allows the user to select a date. See the Hello DatePicker tutorial.
TimePickerDialog
A dialog that allows the user to select a time. See the Hello TimePicker tutorial.

If you would like to customize your own dialog, you can extend the base Dialog object or any of the subclasses listed above and define a new layout. See the section on Creating a Custom Dialog below.

 

下面开始介绍,我们将要看到击中Dialogh ,图中是,我做实验的6种Dialog.

Showing a Dialog

Dialog 是作为Activity的一部分显示.常规的创建一个Dialog需要用Activity的 onCreateDialog(int)方法.如果,你要在该方法外面创建Dialog的话,该Dialog不会与Activity绑定,但你可以用 setOwnerActivity(Activity) 方法绑定.当你想要show一个Dialog的时候,调用showDialog(int)方法,并传给它一个唯一的你想显示的dialog的身份识别.既id.在dialog方法显示之前Android会先调用 onPrepareDialog(int, Dialog) 方法,如果你想每次dialog打开的时候改变dialog的一些属性的话,可以在改方法里面写一些代码. onCreateDialog(int)方法指在第一次打开的时候被调用一次.如果你没定义 onPrepareDialog() 方法,那么dialog会和原先打开的一样.

Creating an AlertDialog

Adding buttons

可以使用如下代码生成.menu中指定相应id,使点击相应的item,就能产生不同的Dialog.我使用的Menu代码如下:

j

在复写的public boolean onOptionsItemSelected(MenuItem item) 方法中,
        用如下代码,产生相应的Dialog

 

Adding a list

以下是使用list的使用代码,在代码中,我也写了些注释,自己不懂的我就查资料后,并注出来.代码如下:

1

Adding checkboxes and radio buttons

以下是使用radio的使用代码,在代码中,我也写了些注释,自己不懂的我就查资料后,并注出来.代码如下:

1

 

 

Creating a ProgressDialog

Showing a progress Wheel

以下是一个progress Wheel的代码:

1

Showing a progress bar

以下是一个progress bar的代码:

2

Creating a Custom Dialog

这个DIalog是个比较让人喜欢的对话框.

开发者可以在layout/下,定义自己的xml文件,并使dialog使用developer自己定义的样式来使用.非常人性化.

用户可在layout下定义如下xml文件.

并在java文件中调用相应的布局.下面是java文件中的代码.但是我在做这个实验的时候,遇到个问题,就是使用文档中的getApplicationContext方法

,并调用相应的方法

,程序不出错,但是放到模拟器下,点击menu 的item,程序就报错了.

所以看到这个文档的同学,如果你清楚为什么的话,留言给我哈.

我遇到有问题的代码是:

然后我将上面的代码做了如下修改,并运行,最终则没问题了.

我想知道的是,两种代码之间的不同和产出错误的原因.希望知道的同学,不要吝惜你的几行留言.

本文测试代码csdn下载频道:http://download.youkuaiyun.com/source/2903639

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值