安卓弹出对话框——AlertDialog(二)

在Android中,启动一个对话框有三种方式:

1、定义一个新的activity,并将其主题设置为对话框风格

2、使用AlertDialog类,并且显示它

3、使用 Android的Dialog类的子类,并且显示它

 

 

现在学习AlertDialog.Builder创建各种形式的对话框。

首先,看看启动界面如下:

clip_image002

用土司来显示效果,因为多次用到,所以将其抽象为一个方法。

 

[java] view plaincopy
 
  1. protected void showToast(String string) {  
  2.     Toast.makeText(this, string, Toast.LENGTH_SHORT).show();  
  3. }  

 

 

 

1、点击第一个按钮后,出现如下对话框:

image

对于这个对话框,我们用到了AlertDialog.Builder类的几个方法:

setTitle:设置标题

setIcon:设置图标

setMessage:设置文本

setPositiveButton:设置第一个按钮

setNeutralButton:第二个按钮

setNegativeButton:第三个按钮

 

 

本段代码如下:

 

[java] view plaincopy
 
  1. public void onClick(View v) {  
  2.     switch (v.getId()) {  
  3.     case R.id.button1:  
  4.         new AlertDialog.Builder(this)  
  5.             .setTitle("这是一个最简单的对话框")  
  6.             .setIcon(R.drawable.img1)  
  7.             .setMessage("你好!!!")  
  8.             .setPositiveButton("开始", new DialogInterface.OnClickListener() {  
  9.                 @Override  
  10.                 public void onClick(DialogInterface dialog, int which) {  
  11.                     DialogActivity.this.showToast("你点击了开始按钮      "+ which);  
  12.                 }  
  13.             })  
  14.             .setNeutralButton("暂停", new DialogInterface.OnClickListener() {  
  15.                 @Override  
  16.                 public void onClick(DialogInterface dialog, int which) {  
  17.                     DialogActivity.this.showToast("你点击了暂停按钮      "+ which);  
  18.                 }  
  19.             })  
  20.               
  21.             .setNegativeButton("退出", new DialogInterface.OnClickListener() {  
  22.                 @Override  
  23.                 public void onClick(DialogInterface dialog, int which) {  
  24.                     DialogActivity.this.showToast("你点击了退出按钮 " + which);  
  25.                 }  
  26.             }).show();  
  27.         break;  

 

 

 

 

2、第二个对话框效果如下图

image image

对于这个对话框,我们用到了这个方法

setItem(),即将setMessage改成这个方法就可以了。

 

代码如下:其中items是一个成员变量。final String[] items = {"开始","暂停","退出"};

 

[java] view plaincopy
 
  1. case R.id.button2:  
  2.     new AlertDialog.Builder(this)  
  3.     .setTitle("选项列表对话框")  
  4.     .setIcon(R.drawable.img2)  
  5.     //items的第一个参数也可以接受itemID,所以可写在xml文件中  
  6.     .setItems(items, new DialogInterface.OnClickListener() {  
  7.         @Override  
  8.         public void onClick(DialogInterface dialog, int which) {  
  9.             DialogActivity.this.showToast("你点击了按钮 " + items[which]);  
  10.         }  
  11.     }).show();  
  12. break;  

 

 

 

 

 

3、第三个对话框:

image

setSingleChoiceItem,设置单选列表对话框

 

代码如下:

 

[java] view plaincopy
 
  1. case R.id.button3:  
  2.     new AlertDialog.Builder(this)  
  3.     .setTitle("带单选框的列表对话框")  
  4.     .setIcon(R.drawable.img3)    
  5.     //setSingleChoiceItems()的第二个参数是设置默认选项,选项索引从0开始,-1代表不选择任何选项。  
  6.     .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {  
  7.         @Override  
  8.         public void onClick(DialogInterface dialog, int which) {  
  9.             DialogActivity.this.showToast("你点击了按钮 " + items[which]);  
  10.             //需调用这个方法,使点击后对话框消失,不然一直不会消失的  
  11.             dialog.cancel();    
  12.         }  
  13.     }).create().show();  
  14. break;  

 

 

 

4、第四个对话框:

image

主要用到了方法:

setMultiChoiceItems

 

代码如下:

 

[java] view plaincopy
 
  1. case R.id.button4:  
  2.     new AlertDialog.Builder(this)  
  3.     .setTitle("你的爱好有:")  
  4.     .setIcon(R.drawable.img3)  
  5.     .setMultiChoiceItems(R.array.string_array_name, null, new DialogInterface.OnMultiChoiceClickListener() {  
  6.         @Override  
  7.         public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  8.             String[] array = DialogActivity.this.getResources().getStringArray(R.array.string_array_name);  
  9.             String str;  
  10.             if(isChecked) {  
  11.                 number++;  
  12.             }else {  
  13.                 number--;  
  14.             }  
  15.             DialogActivity.this.showToast(array[which] + (isChecked ?" 选中了":" 取消了") );  
  16.         }  
  17.     })  
  18.     .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(DialogInterface dialog, int which) {  
  21.                 DialogActivity.this.showToast("你一共选择了      "+ number + "项");  
  22.             }  
  23.         }).show();  
  24. break;  

 

 

这次,并没有直接用数组,而是在strings.xml中定义的一个数组资源

 

[xhtml] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.      <string-array  name="string_array_name">  
  4.         <item>健美操</item>  
  5.         <item>跳舞</item>  
  6.         <item>跑步</item>  
  7.     </string-array>  
  8. </resources>  

 

 

 

 

5、第5个对话框:

image

setView(view)方法来显示登录框。接受的参数为View(view,editText的组合),以LayoutInflater来实现。

要得到LayoutInflater(布局泵),只需要调用

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我们用inflater .inflater()用来找layout下xml布局文件,并且实例化。

类似于findVIewbyID,区别是一个得到整个布局,一个得到单个的组件。

 

代码如下:

 

 

 

[java] view plaincopy
 
  1. case R.id.button5:  
  2.     LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  3.     View view = inflater.inflate(R.layout.dialog, null);  
  4.   
  5.     new AlertDialog.Builder(this)  
  6.     .setTitle("登陆框")  
  7.     .setIcon(R.drawable.img4)  
  8.     .setView(view)  
  9.     .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  10.         @Override  
  11.         public void onClick(DialogInterface dialog, int which) {  
  12.             DialogActivity.this.showToast("正在登录,请稍后。。。");  
  13.         }  
  14.     }).show();  

 

 

 

 

 

 

res/layout/dialog.xml布局为两个TextView,两个EditText,如下:

 

[xhtml] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7.     <TextView    
  8.     android:layout_width="wrap_content"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="账号"  
  11.      
  12.     />  
  13.   <EditText    
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     android:id="@+id/username"  
  17.     />  
  18.    <TextView    
  19.     android:layout_width="wrap_content"   
  20.     android:layout_height="wrap_content"   
  21.     android:text="密码"  
  22.     />  
  23.     <EditText    
  24.     android:layout_width="fill_parent"   
  25.     android:layout_height="wrap_content"   
  26.     android:id="@+id/password"  
  27.     />  
  28. </LinearLayout>  

 

【转】http://blog.csdn.net/kuangc2008/article/details/6358915

 

 

 

 

posted @ 2014-07-09 22:17  伟大的臭猪猪  阅读(968)  评论(2编辑  收藏  举报