Android_文档学习_UI_Creating Dialogs

Creating Dialogs

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

本文测试代码csdn下载频道:http://download.csdn.net/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代码如下:

[java]  view plain copy
  1. @Override  
  2.     public boolean onCreateOptionsMenu(Menu menu) {  
  3.         // TODO Auto-generated method stub  
  4.         menu.add(011"AlertDialog");  
  5.         menu.add(022"ListDialog");  
  6.         menu.add(033"RadioDialog");  
  7.         menu.add(044"ProgressWheelDialog");  
  8.         menu.add(055"ProgressBarDialog");  
  9.         menu.add(066"CustomDialog");  
  10.         return super.onCreateOptionsMenu(menu);  
  11.     }  
j

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

[java]  view plain copy
  1. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  2.             builder.setMessage("Are you sure you want to exit?")  
  3.                    .setCancelable(false)            //设置此项为false ,则用户按返回按钮,不会关闭这个alertDialog  
  4.                    .setPositiveButton("Yes"new DialogInterface.OnClickListener() {      
  5.                                                     //在AlertDialog中添加按钮要实现一些功能,必须先实现DialogInterface.OnClickListener接口  
  6.                        public void onClick(DialogInterface dialog, int id) {  
  7.                             finish();  
  8.                        }  
  9.                    })  
  10.                    .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  11.                        public void onClick(DialogInterface dialog, int id) {  
  12.                             dialog.cancel();  
  13.                        }  
  14.                    });  
  15.             AlertDialog alertDialog = builder.create();  
  16.             alertDialog.show();  

 

Adding a list

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

[java]  view plain copy
  1. final CharSequence[] itemsList = {"Red""Green""Blue"};  
  2.             AlertDialog.Builder builderList = new AlertDialog.Builder(this);  
  3.             builderList.setTitle("Pick a color");  
  4.             builderList.setItems(itemsList, new DialogInterface.OnClickListener() {//重点是setItems方法  
  5.                 public void onClick(DialogInterface dialog, int item) {  
  6.                     Toast.makeText(getApplicationContext(), itemsList[item], Toast.LENGTH_SHORT).show();  
  7.                 }  
  8.             });  
  9.             AlertDialog alertList = builderList.create();  
  10.             alertList.show();  
1

Adding checkboxes and radio buttons

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

[java]  view plain copy
  1. final CharSequence[] itemsRadio = {"Red""Green""Blue"};  
  2.             AlertDialog.Builder builderRadio = new AlertDialog.Builder(this);  
  3.             builderRadio.setTitle("Pick a color");  
  4.             builderRadio.setSingleChoiceItems(itemsRadio, -1new DialogInterface.OnClickListener() {  
  5.                         //重点是setSingleChoiceItems方法  
  6.                         //Use "-1" to indicate that no item should be selected by default.(方法中的-1,用于设置默认没有选项被选中)  
  7.                 public void onClick(DialogInterface dialog, int item) {  
  8.                     Toast.makeText(getApplicationContext(), itemsRadio[item], Toast.LENGTH_SHORT).show();  
  9.                 }  
  10.             });  
  11.             AlertDialog alertRadio = builderRadio.create();  
  12.               
  13.             alertRadio.show();  
1

 

 

Creating a ProgressDialog

Showing a progress Wheel

以下是一个progress Wheel的代码:

[java]  view plain copy
  1. ProgressDialog dialogProgressWheel = ProgressDialog.show(this"",   
  2.                     "Loading. Please wait..."true);  
  3.             dialogProgressWheel.setCancelable(true);//我将这项设为true是因为实验中,不能按返回,回到主界面是件很不爽的事  
  4.             dialogProgressWheel.show();  
1

Showing a progress bar

以下是一个progress bar的代码:

[java]  view plain copy
  1. ProgressDialog progressBarDialog;  
  2.             progressBarDialog = new ProgressDialog(this);  
  3.             progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  4.             progressBarDialog.setMessage("Loading...");  
  5.             progressBarDialog.setCancelable(false);  
  6.             progressBarDialog.setCancelable(true);  
  7.             progressBarDialog.show();  
2

Creating a Custom Dialog

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

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

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

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:id="@+id/layout_root"  
  4.               android:orientation="horizontal"  
  5.               android:layout_width="fill_parent"  
  6.               android:layout_height="fill_parent"  
  7.               android:padding="10dp"  
  8.               >  
  9.     <ImageView android:id="@+id/image"  
  10.                android:layout_width="wrap_content"  
  11.                android:layout_height="fill_parent"  
  12.                android:layout_marginRight="10dp"  
  13.                />  
  14.     <TextView android:id="@+id/text"  
  15.               android:layout_width="wrap_content"  
  16.               android:layout_height="fill_parent"  
  17.               android:textColor="#FFF"  
  18.               />  
  19. </LinearLayout>  

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

,并调用相应的方法

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

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

我遇到有问题的代码是:

[java]  view plain copy
  1. Context mContext = getApplicationContext();  
  2. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);   

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

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

[java]  view plain copy
  1. //Context mContext = getApplicationContext();  
  2.             LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值