【Android】对话框 AlertDialog

简介:

【Android】对话框 AlertDialog


本讲介绍一下Android基本组件:对话框AlertDialog。

 来源:http://blog.csdn.net/feng88724/article/details/6171450

API:

java.lang.Object
   ↳ android.app.AlertDialog.Builder

 

使用AlertDialog.Builder创建对话框需要了解以下几个方法:

  • setTitle :为对话框设置标题
  • setIcon :为对话框设置图标
  • setMessage:为对话框设置内容
  • setView : 给对话框设置自定义样式
  • setItems :设置对话框要显示的一个list,一般用于显示几个命令时。
  • setMultiChoiceItems :用来设置对话框显示一系列的复选框。
  • setNeutralButton    :
  • setPositiveButton   :给对话框添加"Yes"按钮
  • setNegativeButton :对话框添加"No"按钮
  • create : 创建对话框
  • show :显示对话框

 

下面我们来看一下最简单对话框。

 


这个对话框只是简单的显示内容,使用默认图标,没有按钮,不多说,贴代码:


[java]  view plain copy print ?
  1. new AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示标题").setMessage("这是提示内容").show();   
 

(Lesson_02_Dia是类名,请换成自己的!!)

 

 

下面我们为这个对话框加个按钮,效果:

 

 

代码:

[java]  view plain copy print ?
  1. new AlertDialog.Builder(Lesson_01_Pic.this)  
  2.          .setTitle("这是标题")  
  3.         .setMessage("这是提示内容")   
  4.         .setPositiveButton("确定",   
  5.         new DialogInterface.OnClickListener(){  
  6.                   public void onClick(DialogInterface dialoginterface, int i){   
  7.                                  //按钮事件   
  8.                             Toast.makeText(Lesson_01_Pic.this"确定",Toast.LENGTH_LONG).show();  
  9.                               }   
  10.                       }).show();   
 

 

添加按钮时,需要同时为该按钮指定监听器。

 

 

下面,我们修改一个图标,添加两个按钮,同时显示多个选项,先看下效果:

 

代码:

[c-sharp]  view plain copy print ?
  1. package com.yfz;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.app.Dialog;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnClickListener;  
  7. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.Toast;  
  12. public class Lesson_02_Dia extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         Button button = (Button)findViewById(R.id.b01);  
  20.         button.setText("对话框");  
  21.         button.setOnClickListener(new Button.OnClickListener(){  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 //选项数组  
  25.                 String[] choices={"Facebook","Twitter"};  
  26.                 //Check判断数组,与选项对应  
  27.                 boolean[] chsBool = {true,false};  
  28.                  //包含多个选项及复选框的对话框  
  29.                 AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)  
  30.                          .setIcon(android.R.drawable.btn_star_big_on)  
  31.                          .setTitle("调查")  
  32.                          .setMultiChoiceItems(choices, chsBool, multiClick)  
  33.                          .setPositiveButton("Yes", onclick)  
  34.                          .setNegativeButton("No",  onclick).create();  
  35.                 dialog.show();  
  36.             }  
  37.               
  38.         });  
  39.     }  
  40.       
  41.     /** 
  42.      * 对话框复选框事件监听器 
  43.      */  
  44.     OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){  
  45.         @Override  
  46.         public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  47.             Toast.makeText(Lesson_02_Dia.this"第"+(which+1)+"项,选中结果:"+isChecked,Toast.LENGTH_SHORT).show();  
  48.         }  
  49.           
  50.     };  
  51.       
  52.     /** 
  53.      * 对话框按钮点击事件监听器 
  54.      */  
  55.     OnClickListener onclick = new OnClickListener() {  
  56.         @Override  
  57.         public void onClick(DialogInterface dialog, int which) {  
  58.             switch (which) {  
  59.                 case Dialog.BUTTON_NEGATIVE:  
  60.                     Toast.makeText(Lesson_02_Dia.this"No..",  
  61.                             Toast.LENGTH_LONG).show();  
  62.                     break;  
  63.                 case Dialog.BUTTON_NEUTRAL:  
  64.                     Toast.makeText(Lesson_02_Dia.this"I don't know.",  
  65.                             Toast.LENGTH_LONG).show();  
  66.                     break;  
  67.                 case Dialog.BUTTON_POSITIVE:  
  68.                     Toast.makeText(Lesson_02_Dia.this"Yes!!",  
  69.                             Toast.LENGTH_LONG).show();  
  70.                     break;  
  71.             }  
  72.         }  
  73.     };  
  74. }  
 

 

说明已经写在注释中了。

 

下面再介绍一种比较常用的式样,如图:

 

代码:

[java]  view plain copy print ?
  1.    @Override  
  2.    public void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.        setContentView(R.layout.main);  
  5.          
  6.        Button button = (Button)findViewById(R.id.b01);  
  7.        button.setText("对话框");  
  8.        button.setOnClickListener(new Button.OnClickListener(){  
  9.         @Override  
  10.         public void onClick(View v) {  
  11.             //选项数组  
  12.             String[] choices={"新浪微博","校内","街旁"};  
  13.                  //包含多个选项的对话框  
  14.             AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)  
  15.                      .setIcon(android.R.drawable.btn_star)  
  16.                      .setTitle("分享")  
  17.                      .setItems(choices, onselect).create();  
  18.             dialog.show();  
  19.         }  
  20.        });  
  21.    }  
  22.      
  23.      
  24. /** 
  25.  * 选项的事件监听器 
  26.  */  
  27.    OnClickListener onselect = new OnClickListener() {  
  28.     @Override  
  29.     public void onClick(DialogInterface dialog, int which) {  
  30.         // TODO Auto-generated method stub  
  31.         switch (which) {  
  32.         case 0:  
  33.             Toast.makeText(Lesson_02_Dia.this"您选择了新浪微博.",Toast.LENGTH_SHORT).show();  
  34.             break;  
  35.         case 1:  
  36.             Toast.makeText(Lesson_02_Dia.this"您选择了校内",Toast.LENGTH_SHORT).show();  
  37.             break;  
  38.         case 2:  
  39.             Toast.makeText(Lesson_02_Dia.this"您选择了街旁",Toast.LENGTH_SHORT).show();  
  40.             break;  
  41.     }  
  42.     }  
  43.       
  44.    };  
 

 

好了,今天就写到这,改天写一下,如果在弹出框中做一个登陆界面。

 

 

继续补充...先上图...

 

页面login.xml: 示例写的比较简单,布局大家可以自己完善、修改。

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout  
  3. android:id="@+id/widget36"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. android:orientation="vertical"  
  7. xmlns:android="http://schemas.android.com/apk/res/android"  
  8. >  
  9. <TextView  
  10. android:id="@+id/widget37"  
  11. android:layout_width="wrap_content"  
  12. android:layout_height="wrap_content"  
  13. android:text="用户名:"  
  14. >  
  15. </TextView>  
  16. <EditText  
  17. android:id="@+id/widget38"  
  18. android:layout_width="wrap_content"  
  19. android:layout_height="wrap_content"  
  20. android:text=""  
  21. android:textSize="18sp"  
  22. >  
  23. </EditText>  
  24. <TextView  
  25. android:id="@+id/widget39"  
  26. android:layout_width="wrap_content"  
  27. android:layout_height="wrap_content"  
  28. android:text="密码:"  
  29. >  
  30. </TextView>  
  31. <EditText  
  32. android:id="@+id/widget40"  
  33. android:layout_width="wrap_content"  
  34. android:layout_height="wrap_content"  
  35. android:text=""  
  36. android:textSize="18sp"  
  37. >  
  38. </EditText>  
  39. </TableLayout>  
 

 

代码 : (也比较简单)

[c-sharp]  view plain copy print ?
  1. LayoutInflater factory = LayoutInflater.from(Lesson_02_Dia.this);  
  2. //获得自定义对话框  
  3. View view = factory.inflate(R.layout.login, null);  
  4.   
  5. AlertDialog dialog02 = new AlertDialog.Builder(Lesson_02_Dia.this)  
  6.      .setIcon(android.R.drawable.btn_star)  
  7.      .setTitle("登录")  
  8.        .setView(view)  
  9.        .setPositiveButton("Yes", onclick)  
  10.        .setNegativeButton("No",  onclick).create();  
  11. dialog02.show();  
 

 

有问题欢迎大家交流。

 

相关文章
|
4月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
34 0
|
1月前
|
Android开发
[Android]AlertDialog对话框
[Android]AlertDialog对话框
16 0
|
8月前
|
Android开发
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
101 0
|
8月前
|
Android开发
Android 中AlertDialog警告对话框的使用
Android 中AlertDialog警告对话框的使用
74 0
|
9月前
|
XML Java Android开发
Android 对话框组件 AlertDialog 四种常用方法
Android 对话框组件 AlertDialog 四种常用方法
103 0
|
9月前
|
XML Android开发 数据格式
Android上机实验-4 菜单和对话框
Android上机实验-4 菜单和对话框
112 1
|
10月前
|
Android开发
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
|
XML 存储 前端开发
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
225 0
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
|
Android开发 数据安全/隐私保护
android自定义对话框实现
android自定义对话框
205 0
android自定义对话框实现
|
XML Android开发 数据格式
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色
744 0
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色