Android实战简易教程<六>(各种对话框Dialog用法研究大全)

在图形界面中,对话框也是人机交互的一种重要形式,程序可以通过对话框进行一些信息的提示,而用户也可以通过对话框和程序进行一些简单的交互操作。

在Android中,所有的对话框都是从android.app.Dialog类继承而来的,此类的继承结构如下:

java.lang.Object

   android.app.Dialog

可以发现此类直接继承自Object类,与View类没有任何继承关系。

一、AlertDialog和AlertDialog.Builder

警告框(AlertDialog)是项目中最常见的对话框形式,是Dialog的直接子类。

如果想要实例化AlertDialog类,往往需要依靠其内部类AlertDialog.Builder类完成。

下面通过一个最常见的返回键提示退出功能来演示一下其用法。

1.布局文件main.xml,我们只要研究返回键,所以布局文件可以不设置任何控件。

2.MainActivity.java:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.content.DialogInterface.OnClickListener;  
  8. import android.os.Bundle;  
  9. import android.view.KeyEvent;  
  10. import android.view.View.OnKeyListener;  
  11.   
  12. public class MainActivity extends Activity {  
  13.       
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState); // 生命周期方法  
  16.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  17.   
  18.     }  
  19.     @Override  
  20.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  21.         if(keyCode==KeyEvent.KEYCODE_BACK){  
  22.             this.exitDialog();  
  23.         }  
  24.         return super.onKeyDown(keyCode, event);  
  25.     }  
  26.     private void exitDialog() {  
  27.         Dialog dialog=new AlertDialog.Builder(this).setTitle("程序退出?").setMessage("确定退出吗?").setPositiveButton("退出"new OnClickListener() {  
  28.               
  29.             public void onClick(DialogInterface dialog, int which) {  
  30.                 MainActivity.this.finish();  
  31.                   
  32.             }  
  33.         }).setNegativeButton("取消"new OnClickListener() {  
  34.               
  35.             public void onClick(DialogInterface dialog, int which) {  
  36.                 // TODO Auto-generated method stub  
  37.                   
  38.             }  
  39.         }).create();//创建Dialog  
  40.         dialog.show();//显示对话框  
  41.           
  42.     }  
  43. }  


运行实例如下:

这属于比较简单的应用,但是在实际开发中用到比较多。

二、类似ListView的显示风格的选项列表

这种显示风格要用到setSingleChoiceItems()方法;

1.main.xml代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView  
  6.         android:id="@+id/mych"  
  7.         android:text=""  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" />  
  10.     <TextView  
  11.         android:id="@+id/mytext"  
  12.         android:text=""  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content" />  
  15.     <Button   
  16.         android:id="@+id/mybut"  
  17.         android:text="选择水果"  
  18.         android:layout_width="wrap_content"   
  19.         android:layout_height="wrap_content"/>  
  20. </LinearLayout>   


2.MainActivity.java如下:

[java]  view plain copy
  1. package org.lxh.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class MyDialogDemo extends Activity {  
  14.     private Button mybut = null ;   // 定义按钮  
  15.     private TextView mych = null ;  // 定义文本  
  16.     private TextView mytext = null ;    // 定义文本  
  17.     private String fruitData [] = new String[] { "苹果""西瓜""水蜜桃" };  
  18.     private String fruitDesc [] = new String[] {  
  19.             "苹果,植物类水果,多次花果,具有丰富的营养成分,有食疗、辅助治疗等功能。",  
  20.             "西瓜(学名:Citrullus Lanatus,英文:Watermelon),属葫芦科,原产于非洲。",  
  21.             "水蜜桃,在植物分类学上属于蔷薇科,梅属,桃亚属,为落叶小乔木。"} ;  
  22.     private int chNum = 0 ; // 保存选项  
  23.     @Override   
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         super.setContentView(R.layout.main); // 调用布局管理器  
  27.         this.mybut = (Button) super.findViewById(R.id.mybut) ;  // 取得按钮  
  28.         this.mych = (TextView) super.findViewById(R.id.mych) ;  // 取得文本  
  29.         this.mytext = (TextView) super.findViewById(R.id.mytext) ;  // 取得文本  
  30.         this.mybut.setOnClickListener(new OnClickListenerImpl()) ;  // 设置事件类  
  31.     }  
  32.     private class OnClickListenerImpl implements OnClickListener {  
  33.    
  34.         @Override  
  35.         public void onClick(View view) {  
  36.             Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)  
  37.                 .setIcon(R.drawable.pic_m)   
  38.                 .setTitle("请选择你喜欢吃的水果?")  
  39.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  40.                       
  41.                     @Override  
  42.                     public void onClick(DialogInterface dialog, int which) {  
  43.                                     MyDialogDemo.this.mych  
  44.                                             .setText(MyDialogDemo.this.fruitData[MyDialogDemo.this.chNum]); // 设置选项的名称  
  45.                     }  
  46.                 })  
  47.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  48.                     @Override  
  49.                     public void onClick(DialogInterface dialog, int which) {  
  50.                           
  51.                     }  
  52.                 }).setSingleChoiceItems(MyDialogDemo.this.fruitData, 0new DialogInterface.OnClickListener() {//setSingleChoiceItems()方法  
  53.                     @Override  
  54.                     public void onClick(DialogInterface dialog, int which) {  
  55.                                     MyDialogDemo.this.mytext  
  56.                                             .setText(MyDialogDemo.this.fruitDesc[which]);  
  57.                                     MyDialogDemo.this.chNum = which ;   // 保存选项的索引  
  58.                     }  
  59.                 }).create() ;  
  60.             dialog.show() ;  
  61.         }  
  62.           
  63.     }  
  64.   
  65. }  


运行实例:

三、定制对话框和LayoutInflater

之前的对话框都是直接通过Activity程序进行定义的,但是如果希望在对话框中显示一些复杂的界面,例如编写一个登陆提示对话框,就需要通过布局文件定义显示组件,之后再将这些布局显示包含到对话框中,而如果想要包含,则需要LayoutInflater类的支持。

1定义布局管理器main.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/mybut"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="登录" />  
  12.   
  13. </LinearLayout>  


2.定义对话框所需的布局管理器login.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.      
  7. <LinearLayout   
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:orientation="horizontal" >  
  11.      <TextView  
  12.           
  13.         android:layout_width="60dp"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_weight="1"  
  16.         android:text="用户名:" />  
  17.       <EditText  
  18.         android:id="@+id/edit_user"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_weight="88"  
  22.         android:text="yayun" />  
  23.       
  24.       
  25. </LinearLayout>  
  26. <LinearLayout   
  27.     android:layout_width="fill_parent"  
  28.     android:layout_height="wrap_content"  
  29.     android:orientation="horizontal" >  
  30.      <TextView  
  31.           
  32.         android:layout_width="60dp"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_weight="1"  
  35.         android:text="密码:" />  
  36.       <EditText  
  37.         android:id="@+id/edit_passwd"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_weight="88"  
  41.         android:password="true"  
  42.         android:text="123456" />  
  43.       
  44.       
  45. </LinearLayout>  
  46.   
  47.   
  48. </LinearLayout>  


3.定义MainActivity.java:

[java]  view plain copy
  1. package org.lxh.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class MyDialogDemo extends Activity {  
  16.     private Button btn;  
  17.     String string_userString;  
  18.     String string_passwdString;  
  19.           
  20.     @Override   
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         super.setContentView(R.layout.main); // 调用布局管理器  
  24.         btn=(Button)findViewById(R.id.mybut);  
  25.         btn.setOnClickListener(new OnClickListenerImpl());        
  26.     }  
  27.     private class OnClickListenerImpl implements OnClickListener{  
  28.   
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             LayoutInflater layoutInflater=LayoutInflater.from(MyDialogDemo.this);//获得layoutInflater对象  
  32.             View view=layoutInflater.from(MyDialogDemo.this).inflate(R.layout.login, null);//获得view对象  
  33.             EditText edit_user=(EditText)view.findViewById(R.id.edit_user);//获取控件  
  34.             EditText edit_passwd=(EditText)view.findViewById(R.id.edit_passwd);  
  35.              string_userString=edit_user.getText().toString();  
  36.              string_passwdString=edit_passwd.getText().toString();  
  37.             Dialog dialog=new AlertDialog.Builder(MyDialogDemo.this).setTitle("用户登录").setView(view).setPositiveButton("登录"new DialogInterface.OnClickListener() {  
  38.                   
  39.                 public void onClick(DialogInterface dialog, int which) {  
  40.                     if(string_userString.equals("yayun")&&string_passwdString.equals("123456")){  
  41.                         Toast.makeText(MyDialogDemo.this"登录成功", Toast.LENGTH_SHORT).show();  
  42.                     }else{  
  43.                         Toast.makeText(MyDialogDemo.this"登录失败", Toast.LENGTH_SHORT).show();  
  44.                     }  
  45.                       
  46.                 }  
  47.             }).setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  48.                   
  49.                 @Override  
  50.                 public void onClick(DialogInterface dialog, int which) {  
  51.                     // TODO Auto-generated method stub  
  52.                       
  53.                 }  
  54.             }).create();  
  55.             dialog.show();  
  56.         }  
  57.   
  58.           
  59.           
  60.           
  61.     }  
  62.       
  63.   
  64. }  


4.运行实例:

四、日期对话框(DatePickerDialog)和时间对话框(TimePickerDialog)

主要进行日期和时间的设置。

1.main.xml代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/mybut"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="设置日期" />  
  12.     <Button  
  13.         android:id="@+id/mytext"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="" />  
  17.   
  18. </LinearLayout>  


2.MainActivity.java代码如下:

[java]  view plain copy
  1. package org.lxh.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.DatePickerDialog;  
  5. import android.app.Dialog;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.DatePicker;  
  11. import android.widget.TextView;  
  12.   
  13. public class MyDialogDemo extends Activity {  
  14.     private Button btn;  
  15.     private TextView textView;  
  16.           
  17.     @Override   
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         super.setContentView(R.layout.main); // 调用布局管理器  
  21.         btn=(Button)findViewById(R.id.mybut);  
  22.         textView=(TextView)findViewById(R.id.mytext);  
  23.         btn.setOnClickListener(new OnClickListenerImpl());        
  24.     }  
  25.     private class OnClickListenerImpl implements OnClickListener{  
  26.   
  27.         @Override  
  28.         public void onClick(View v) {  
  29.             Dialog dialog=new DatePickerDialog(MyDialogDemo.thisnew DatePickerDialog.OnDateSetListener() {  
  30.                   
  31.                 @Override  
  32.                 public void onDateSet(DatePicker view, int year, int monthOfYear,  
  33.                         int dayOfMonth) {  
  34.                     textView.setText("更新日期为:"+year+"年-"+(monthOfYear+1)+"月-"+dayOfMonth+"日");  
  35.                       
  36.                 }  
  37.             }, 19900401);  
  38.             dialog.show();  
  39.               
  40.         }  
  41.           
  42.     }  
  43.       
  44.       
  45.   
  46. }  


运行实例:

TimePickerDialog对话框和DatePickerDialog对话框基本一样。

五、进度条对话框

先看一个简单的圈形进度处理对话框,这个在开发中也十分常见,常用于登录等待、网络连接等待、数据刷新等待等,可以增加用户友好度。

1.main.xml代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/mybut"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="查找网络" />  
  12.   
  13. </LinearLayout>  


2.MainActivity.java代码如下:

[html]  view plain copy
  1. package org.lxh.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.DatePickerDialog;  
  5. import android.app.Dialog;  
  6. import android.app.ProgressDialog;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.DatePicker;  
  12. import android.widget.TextView;  
  13.   
  14. public class MyDialogDemo extends Activity {  
  15.     private Button btn;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         super.setContentView(R.layout.main); // 调用布局管理器  
  21.         btn = (Button) findViewById(R.id.mybut);  
  22.         btn.setOnClickListener(new OnClickListenerImpl());  
  23.     }  
  24.   
  25.     private class OnClickListenerImpl implements OnClickListener {  
  26.         @Override  
  27.         public void onClick(View v) {  
  28.             final ProgressDialog dialog = ProgressDialog.show(  
  29.                     MyDialogDemo.this, "请搜索网络...", "请耐心等待...");// 之所以用final定义,主要目的是为了让内部类可以访问方法中定义的参数。  
  30.             new Thread() {  
  31.                 public void run() {  
  32.                     try {  
  33.                         Thread.sleep(3000);  
  34.                     } catch (InterruptedException e) {  
  35.                         // TODO Auto-generated catch block  
  36.                         e.printStackTrace();  
  37.                     } finally {  
  38.                         dialog.dismiss();  
  39.                     }  
  40.                 };  
  41.             }.start();  
  42.               
  43.   
  44.         }  
  45.   
  46.     }  
  47.   
  48. }  


运行实例:

三秒钟后对话框会自动消失!

在默认情况下,进度对话框采用的是环形进度条(STYLE_SPINNER)的显示风格,用户也可以根据需要将进度条设置为水平(STYLE_HORIZONTAL)显示风格。

1.main.xml代码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/mybut"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="查找网络" />  
  12.   
  13. </LinearLayout>  


2.MainActivity.java代码如下:

[java]  view plain copy
  1. package org.lxh.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MyDialogDemo extends Activity {  
  12.     private Button btn;  
  13.     private static final int MAX_PROGRESS=100;//总进度值  
  14.       
  15.   
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         super.setContentView(R.layout.main); // 调用布局管理器  
  20.         btn = (Button) findViewById(R.id.mybut);  
  21.         btn.setOnClickListener(new OnClickListenerImpl());  
  22.     }  
  23.   
  24.     private class OnClickListenerImpl implements OnClickListener {  
  25.         @Override  
  26.         public void onClick(View v) {  
  27.             final ProgressDialog dialog=new ProgressDialog(MyDialogDemo.this);  
  28.             dialog.setTitle("搜索网络...");  
  29.             dialog.setMessage("正在搜索,请耐心等待...");  
  30.             dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//显示样式,水平显示  
  31.             dialog.setMax(MAX_PROGRESS);  
  32.             dialog.setProgress(30);//初始位置  
  33.             dialog.setButton("后台处理"new DialogInterface.OnClickListener() {  
  34.                   
  35.                 @Override  
  36.                 public void onClick(DialogInterface dialog, int which) {  
  37.                     dialog.dismiss();  
  38.                       
  39.                 }  
  40.             });  
  41.             dialog.setButton2("详细信息"new DialogInterface.OnClickListener() {//注意Button2!!  
  42.                   
  43.                 @Override  
  44.                 public void onClick(DialogInterface dialog, int which) {  
  45.                     // TODO Auto-generated method stub  
  46.                       
  47.                 }  
  48.             });  
  49.             dialog.onStart();//启动进度条  
  50.             dialog.show();  
  51.             new Thread(){  
  52.                 public void run() {  
  53.                     for (int i = 0; i <MAX_PROGRESS; i++) {  
  54.                         try {  
  55.                             Thread.sleep(500);  
  56.                         } catch (InterruptedException e) {  
  57.                             // TODO Auto-generated catch block  
  58.                             e.printStackTrace();  
  59.                         }  
  60.                         dialog.incrementProgressBy(1);//自增长1,重要!  
  61.                     }  
  62.                     dialog.dismiss();  
  63.                 };  
  64.             }.start();  
  65.               
  66.   
  67.         }  
  68.   
  69.     }  
  70.   
  71. }  


运行如下:

总结

1.如果想要实例化AlertDialog类,往往需要依靠其内部类AlertDialog.Builder类完成;

2.判断是否是返回键:keyCode==KeyEvent.KEYCODE_BACK;

3.setSingleChoiceItems()方法的使用;

4.LayoutInflater类引入布局文件,LayoutInflater layoutInflater=LayoutInflater.from(MyDialogDemo.this);//获得layoutInflater对象View view=layoutInflater.from(MyDialogDemo.this).inflate(R.layout.login, null);//获得view对象;

5.Dialog 的setView()方法,设置显示布局;

6.日期对话框(DatePickerDialog)和时间对话框(TimePickerDialog);

7.ProgressDialog的setProgressStyle()方法设置等待对话框显示样式,incrementProgressBy()启动自增长。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值