Android使用对话框实现

<

div id=”content” contentScore=”878″>Android为程序员准备的四种类型的Dialog:AlertDialog、ProgressDialog、DatePickerDialog、TimePickerDialog。

使用它们是很简单的。我们经常看到,当我们按下退出键时会出来一个提示框。我们先看看它的实现。

  1. import android.app.Activity;  
  2. import android.app.AlertDialog;  
  3. import android.app.Dialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6. import android.widget.Button;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9.   
  10. public class AlertDialogActivity extends Activity implements OnClickListener{  
  11.     /** Called when the activity is first created. */  
  12.     Button button;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         button = (Button)findViewById(R.id.exitbutton);  
  18.         button.setOnClickListener(this);  
  19.     }  
  20.     @Override  
  21.     public void onClick(View v) {  
  22.         ExitDialog();  
  23.     }  
  24.     private void ExitDialog(){  
  25.         AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);  
  26.         builder.setTitle(“Exit Confirm”).setIcon(R.drawable.icon)  
  27.             .setMessage(“Are you sure to Exit programe?”)  
  28.             .setPositiveButton(“确定”new DialogInterface.OnClickListener() {  
  29.                 @Override  
  30.                 public void onClick(DialogInterface dialog, int which) {  
  31.                     dialog.dismiss();  
  32.                     AlertDialogActivity.this.finish();  
  33.                 }  
  34.             }).setNegativeButton(“取消”new DialogInterface.OnClickListener(){  
  35.                 @Override  
  36.                 public void onClick(DialogInterface dialog, int which) {  
  37.                     dialog.dismiss();  
  38.                 }  
  39.             });  
  40.         Dialog dialog = builder.create();  
  41.         dialog.show();  
  42.     }  
  43. }  

上面的代码还不算太糟糕,想必大家熟练后会直接 new 一个 AlertDialog.Builder对象:

  1. new AlertDialog.Builder(myActivity.this).setTitle().setMessage().…….create().show();  

有时我们需要添加一个列表,供用户选择:

  1. final CharSequence[] items = {“Red”“Green”“Blue”};  
  2.   
  3. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  4. builder.setTitle(“Pick a color”);  
  5. builder.setItems(items, new DialogInterface.OnClickListener() {  
  6.     public void onClick(DialogInterface dialog, int item) {  
  7.         Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();  
  8.     }  
  9. });  
  10. AlertDialog alert = builder.create();  

也许会设计成单选式样,看起开明朗一点:

  1. final CharSequence[] items = {“Red”“Green”“Blue”};  
  2.   
  3. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  4. builder.setTitle(“Pick a color”);  
  5. builder.setSingleChoiceItems(items, –1new DialogInterface.OnClickListener() {  
  6.     public void onClick(DialogInterface dialog, int item) {  
  7.         Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();  
  8.     }  
  9. });  
  10. AlertDialog alert = builder.create();  

以上是Android dev Guide上的源码。不需要多余的解释。下面看看建立一个自定义的对话框需要的步骤:

1:为自定义的对话框准备一个布局Layout;

  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout  
  3.   xmlns:android=“http://schemas.android.com/apk/res/android”  
  4.   android:orientation=“vertical”  
  5.   android:layout_width=“match_parent”  
  6.   android:layout_height=“match_parent”>  
  7.     <TextView   
  8.     android:id=“@+id/textview”  
  9.     android:layout_width=“fill_parent”  
  10.     android:layout_height=“wrap_content”  
  11.     android:text=“请输入文本”  
  12.     />  
  13.     <EditText   
  14.     android:id=“@+id/edittext”  
  15.     android:layout_width=“fill_parent”  
  16.     android:layout_height=“wrap_content”  
  17.     android:autoText=“false”  
  18.     />  
  19. LinearLayout>  

2:将这个布局导入到一个View 中;

3:创建一个Builder 对象;

4:设置成我们的视图View;

  1. LayoutInflater inflater = LayoutInflater.from(ctx);  
  2. view = inflater.inflate(R.layout.dialog, null);  
  3. AlertDialog.Builder builder = new AlertDialog.Builder(ctx);  
  4. builder.setView(view);  

5:设置按钮 和 监听器;

  1. builder.setTitle(“这是一个自定义的对话框”);  
  2. builder.setPositiveButton(“确定”new DialogInterface.OnClickListener() {  
  3.     @Override  
  4.     public void onClick(DialogInterface dialog, int which) {  
  5.         EditText edittext = (EditText)view.findViewById(R.id.edittext);  
  6.         enterText = edittext.getText().toString();  
  7.         button.setText(enterText);  
  8.         }  
  9.     });  

6:用Builder 创建一个Dialog;

7:显示这个Dialog。

  1. AlertDialog dialog = builder.create();  
  2. builder.show();  

下面是全部代码。很随意,很简单,所以很杂乱。

<

div>

<

ol>

  • import android.app.Activity;  
  • import android.app.AlertDialog;  
  • import android.content.Context;  
  • import android.content.DialogInterface;  
  • import android.os.Bundle;  
  • import android.widget.Button;  
  • import android.widget.EditText;  
  • import android.view.LayoutInflater;  
  • import android.view.View;  
  • public class MyDialogActivity extends Activity implements View.OnClickListener{  
  •     /** Called when the activity is first created. */  
  •     Button button;  
  •     @Override  
  •     public void onCreate(Bundle savedInstanceState) {  
  •         super.onCreate(savedInstanceState);  
  •         setContentView(R.layout.main);  
  •         button = (Button)findViewById(R.id.button);  
  •         button.setOnClickListener(this);  
  •     }  
  •     @Override  
  •     public void onClick(View v) {  
  •         TextDialog textDialog = new TextDialog((Button)v,this);//我将Button传了进去,这是获得修改文本的方法   
  •     }  
  •     private class TextDialog extends AlertDialog{  
  •         private String enterText;  
  •         private Context ctx;  
  •         private View view;  
  •         Button button;  
  •         protected TextDialog(Button btn,Context context) {  
  •             super(context);  
  •             ctx = context;  
  •             button = btn;  
  •         LayoutInflater inflater = LayoutInflater.from(ctx);  
  •         view = inflater.inflate(R.layout.dialog, null);  
  •         AlertDialog.Builder builder = new AlertDialog.Builder(ctx);  
  •         builder.setView(view);  
  •         builder.setTitle(“这是一个自定义的对话框”);  
  •         builder.setPositiveButton(“确定”new DialogInterface.OnClickListener() {  
  •             @Override  
  •             public void onClick(DialogInterface dialog, int which) {  
  •                 EditText edittext = (EditText)view.findViewById(R.id.edittext);  
  •                 enterText = edittext.getText().toString();  
  •                 button.setText(enterText);//点击确定后,按钮的文本会被修改   
  •                 }  
  •             });  
  •         AlertDialog dialog = builder.create();  
  •     ¼/div>