Dialog:对话框,对话框有很多种,系统有已经定义好的Dialog,如前面讲的TimePicker和DatePicker都是系统封装好的,可以直接调用就可以的。现在我们来自己定义自己的Dialog。

效果:

开始定义的两个按钮

android之Dialog_android Dialog

 

Dialog1,这个对话框只定义了一个按钮,没有设置取消按钮

android之Dialog_android Dialog_02

 

Dialog2,这个对话框设置了取消按钮

android之Dialog_android Dialog_03

 

 

layout中的xml文件

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.               android:layout_width="fill_parent" 
  3.               android:layout_height="fill_parent" 
  4.               android:orientation="vertical"> 
  5.  
  6.     <TextView 
  7.         android:layout_width="fill_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:text="DialogActivatyTest" /> 
  10.  
  11.     <Button   
  12.         android:id="@+id/alert1Dialog" 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" 
  15.         android:text="Alert1Dialog" 
  16.         android:textSize="18dp"/> 
  17.     <Button   
  18.         android:id="@+id/alert2Dialog" 
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content" 
  21.         android:text="Alert2Dialog" 
  22.         android:textSize="18dp"/> 
  23.       
  24.       
  25. </LinearLayout> 

 

 

Activaty.java文件

 

  1. package com.cheng.dialogproject;  
  2.  
  3. import android.os.Bundle;  
  4. import android.R.integer;  
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.app.Dialog;  
  8. import android.content.DialogInterface;  
  9. import android.content.DialogInterface.OnClickListener;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.Toast;  
  14.  
  15. public class DialogActivity extends Activity {  
  16.     //定义两个按钮,分别启动不同格式的Dialog  
  17.     private Button alert1Button ,alert2bButton;  
  18.     //设置两种Dialog的id  
  19.     private static final int ALERT_DIALOG_1 = 1;  
  20.     private static final int ALERT_DIALOG_2 = 2;  
  21.       
  22.     protected Dialog onCreateDialog(int id) {  
  23.         Dialog mdiaDialog;  
  24.         AlertDialog.Builder mBuilder;//用来建设自定义的Dialog  
  25.         switch (id) {  
  26.         case ALERT_DIALOG_1:  
  27.             //初始化Builder  
  28.             mBuilder = new AlertDialog.Builder(DialogActivity.this);  
  29.             //setMessage设置Dialog的标题  
  30.             //setcancelable设置按回车键是否退出对话框  
  31.             //setPositiveButton设置左边的按钮  
  32.             mBuilder.setMessage("自己定义的对话框1怎么样?").setCancelable(false)  
  33.             .setPositiveButton("好"new OnClickListener() {  
  34.                 //设置左边按钮的监听器  
  35.                 @Override 
  36.                 public void onClick(DialogInterface dialog, int which) {  
  37.                     // TODO Auto-generated method stub  
  38.                     Toast.makeText(DialogActivity.this"你点击了好!", Toast.LENGTH_LONG).show();  
  39.                 }  
  40.             });  
  41.             //创建Dialog  
  42.             mdiaDialog = mBuilder.create();  
  43.             break;  
  44.         case ALERT_DIALOG_2:  
  45.             mBuilder = new AlertDialog.Builder(DialogActivity.this);  
  46.             //设置按返回键是否能推出对话框  
  47.             mBuilder.setCancelable(false);  
  48.             //setNegativeButton设置取消按钮  
  49.             mBuilder.setMessage("自己定义的对话框2比对话框1好?")  
  50.             .setPositiveButton("是的"new OnClickListener() {  
  51.                   
  52.                 @Override 
  53.                 public void onClick(DialogInterface dialog, int which) {  
  54.                     // TODO Auto-generated method stub  
  55.                     Toast.makeText(DialogActivity.this"你点击了是!", Toast.LENGTH_LONG).show();  
  56.                 }  
  57.             }).setNegativeButton("不是"new OnClickListener() {  
  58.                 //设置取消的监听器  
  59.                 @Override 
  60.                 public void onClick(DialogInterface dialog, int which) {  
  61.                     // TODO Auto-generated method stub  
  62.                       
  63.                 }  
  64.             })  
  65.             ;  
  66.             mdiaDialog  = mBuilder.create();  
  67.             break;  
  68.         default:  
  69.             mdiaDialog = null;  
  70.             break;  
  71.         }  
  72.         return mdiaDialog;  
  73.           
  74.     }  
  75.     @Override 
  76.     protected void onCreate(Bundle savedInstanceState) {  
  77.         super.onCreate(savedInstanceState);  
  78.         super.setContentView(R.layout.main);  
  79.           
  80.         alert1Button = (Button)findViewById(R.id.alert1Dialog);  
  81.         alert2bButton = (Button)findViewById(R.id.alert2Dialog);  
  82.           
  83.         View.OnClickListener  ocl= new View.OnClickListener() {  
  84.             //设置监听器,通过Dialog的id来显示我们点击的Dialog  
  85.             @Override 
  86.             public void onClick(View v) {  
  87.                 // TODO Auto-generated method stub  
  88.                 switch (v.getId()) {  
  89.                 case R.id.alert1Dialog:  
  90.                     //显示对话框  
  91.                     showDialog(ALERT_DIALOG_1);  
  92.                     break;  
  93.                       
  94.                 case R.id.alert2Dialog:  
  95.                     showDialog(ALERT_DIALOG_2);  
  96.                     break;  
  97.                 default:  
  98.                     break;  
  99.                 }  
  100.             }  
  101.         };  
  102.         //绑定监听器  
  103.         alert1Button.setOnClickListener(ocl);  
  104.         alert2bButton.setOnClickListener(ocl);  
  105.     }  
  106.  
  107.     @Override 
  108.     public boolean onCreateOptionsMenu(Menu menu) {  
  109.         // Inflate the menu; this adds items to the action bar if it is present.  
  110.         getMenuInflater().inflate(R.menu.main, menu);  
  111.         return true;  
  112.     }  
  113.  
  114. }  

 

 

源代码下载:

http://down.51cto.com/data/675022