Andriod 对话框

来源:互联网 发布:vb中ucase是什么意思 编辑:程序博客网 时间:2024/05/18 02:07

在Activity中可以调用showDialog()来显示一个对话框,覆盖Activity的onCreateDialog方法,在这个方法中创建对话框,返回一个Dialog对象。

1.最简单的对话框

[java] view plaincopyprint?
  1. AlertDialog.Builder b=new  AlertDialog.Builder(this);  
  2. b.setTitle("简单的");  
  3.             b.setMessage("this is a simple dialog");  
  4.             b.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  5.                   
  6.                 @Override  
  7.                 public void onClick(DialogInterface dialog, int which) {  
  8.                     // TODO Auto-generated method stub  
  9.                       
  10.                 }  
  11.             });  
  12.             b.setNegativeButton("否"new DialogInterface.OnClickListener() {  
  13.                   
  14.                 @Override  
  15.                 public void onClick(DialogInterface dialog, int which) {  
  16.                     // TODO Auto-generated method stub  
  17.                       
  18.                 }  
  19.             });  
  20.             return b.create();  


效果如下

2.列表对话框

[java] view plaincopyprint?
  1. b.setTitle("列表");  
  2.             //b.setMessage("message");这行代码不要有  
  3.             b.setItems(items, new DialogInterface.OnClickListener() {  
  4.                   
  5.                 @Override  
  6.                 public void onClick(DialogInterface dialog, int which) {  
  7.                     // TODO Auto-generated method stub  
  8.                     Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();  
  9.                       
  10.                 }  
  11.             });  
  12.             return b.create();  

items是一个String数组
效果图

3.单选对话框

[java] view plaincopyprint?
  1. b.setTitle("请选择颜色");  
  2.             b.setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {  
  3.                   
  4.                 @Override  
  5.                 public void onClick(DialogInterface dialog, int which) {  
  6.                     // TODO Auto-generated method stub  
  7.                     Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();  
  8.                       
  9.                 }  
  10.             });  
  11.             b.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  12.                   
  13.                 @Override  
  14.                 public void onClick(DialogInterface dialog, int which) {  
  15.                     // TODO Auto-generated method stub  
  16.                       
  17.                 }  
  18.             });  
  19.             b.setNegativeButton("否"new DialogInterface.OnClickListener() {  
  20.                   
  21.                 @Override  
  22.                 public void onClick(DialogInterface dialog, int which) {  
  23.                     // TODO Auto-generated method stub  
  24.                       
  25.                 }  
  26.             });  
  27.             return b.create();  
  28.           

效果图

4.多选对话框

 

[java] view plaincopyprint?
  1. boolean []ddd=new boolean[3];  
  2.             b.setTitle("请选择颜色");  
  3.             b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){  
  4.   
  5.                 @Override  
  6.                 public void onClick(DialogInterface dialog, int which,  
  7.                         boolean isChecked) {  
  8.                     // TODO Auto-generated method stub  
  9.                       
  10.                 }  
  11.                   
  12.             });  
  13.               
  14.             return b.create();  


效果图


5.进度条对话框

[java] view plaincopyprint?
  1. Handler hand=new Handler(){  
  2.   
  3.         @Override  
  4.         public void handleMessage(Message msg) {  
  5.             // TODO Auto-generated method stub  
  6.             super.handleMessage(msg);  
  7.             if(progressint>=100)  
  8.             {  
  9.                 pd.dismiss();  
  10.             }  
  11.             else  
  12.             {  
  13.                 progressint++;  
  14.                 pd.setProgress(progressint);  
  15.                 hand.sendEmptyMessageDelayed(0100);  
  16.             }  
  17.               
  18.         }  
  19.           
  20.     };  
  21. pd=new ProgressDialog(this);  
  22.             pd.setTitle("进度对话框");  
  23.             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  24.             pd.setMax(100);  
  25.             AndroidDialogActivity.this.hand.sendEmptyMessage(0);  
  26.             pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定"new DialogInterface.OnClickListener(){  
  27.   
  28.                 @Override  
  29.                 public void onClick(DialogInterface dialog, int which) {  
  30.                     // TODO Auto-generated method stub  
  31.                       
  32.                       
  33.                 }});  
  34.             return pd;  


效果图

6.代码自定义对话框

[html] view plaincopyprint?
  1. EditText et=new EditText(this);  
  2.             et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);  
  3.             b.setTitle("请输入密码");  
  4.             b.setView(et);  
  5.             return b.create();  


效果图

7.XML文件自定义对话框

xml文件

[html] view plaincopyprint?
  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.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="用户名" />  
  12.     <EditText  
  13.        android:id="@+id/EditText1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="TextView"  
  17.           
  18.         />  
  19.   
  20.     <TextView  
  21.         android:id="@+id/textView2"  
  22.          android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="密码" />  
  25.   
  26.     <EditText  
  27.        android:id="@+id/EdiText2"  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="TextView"  
  31.         android:inputType="textPassword"  
  32.           
  33.         />  
  34.     <Button  
  35.         android:id="@+id/buttonyes"  
  36.          android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:text="确定" />  
  39.   
  40. </LinearLayout>  


Java代码

[java] view plaincopyprint?
  1. b.setIcon(R.drawable.ic_launcher);  
  2.             b.setTitle("自定义对话框");  
  3.             LayoutInflater li=LayoutInflater.from(this);  
  4.             View v=li.inflate(R.layout.info, null);  
  5.             Button yes=(Button) v.findViewById(R.id.buttonyes);  
  6.             yes.setOnClickListener(new OnClickListener(){  
  7.   
  8.                 @Override  
  9.                 public void onClick(View v) {  
  10.                     // TODO Auto-generated method stub  
  11.                     Toast.makeText(AndroidDialogActivity.this"Hello World", Toast.LENGTH_SHORT).show();  
  12.                 }});  
  13.             b.setView(v);  
  14.             return b.create();  


效果图

 下载工程:下载

转自:http://blog.csdn.net/zhy_cheng/article/details/8003467

转自:http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html

原创粉丝点击