简单对话框、选项对话框和Toast

From:http://blog.csdn.net/dazlly/article/details/7869204#

首先简单对话框,分为三部分:标题,内容和下面的按键,先看没有按键的对话框的声明方式:

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

         对话框一共有三个按键,分别对应肯定否定和中立:

[java] view plain copy print ?
  1. public AlertDialog.Builder setPositiveButton (CharSequence text, DialogInterface.OnClickListener listener)   
  2. public AlertDialog.Builder setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)  
  3. public AlertDialog.Builder setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)  
public AlertDialog.Builder setPositiveButton (CharSequence text, DialogInterface.OnClickListener listener) 
public AlertDialog.Builder setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
public AlertDialog.Builder setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)

        具体点击哪个按钮触发的事件在DialogInterface.OnClickListener对象里面编写,所以完整的简单对话框就是在第一条代码基础上再加上某个或某几个按键:

[java] view plain copy print ?
  1. new AlertDialog.Builder(Main.this).setTitle("标题").setMessage("内容")  
  2.                 .setPositiveButton("OK"new DialogInterface.OnClickListener() {  
  3.                     @Override  
  4.                     public void onClick(DialogInterface dialog, int which) {  
  5.                         //这里编写点击对话框按钮后触发的事件   
  6.                     }  
  7.                 }).show();  
new AlertDialog.Builder(Main.this).setTitle("标题").setMessage("内容")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						//这里编写点击对话框按钮后触发的事件
					}
				}).show();

       简单对话框很简单,然后继续学习选项对话框,先想象一下选项对话框和简单对话框的不同,好像就是中间的内容格式不同和没有下面的三个按钮,就是这个了,设置选项和点击监控:

[java] view plain copy print ?
  1. public AlertDialog.Builder setItems (CharSequence[] items, DialogInterface.OnClickListener listener)  
public AlertDialog.Builder setItems (CharSequence[] items, DialogInterface.OnClickListener listener)

        第二个参数已经在简单对话框中学习过是定义点击事件的触发的,那么第一个参数是什么意思,又要从哪里获取呢?其实,这个参数就是我们说要展示的选项对话框内的“选项”,是从values/*.xml中获取的:

[html] view plain copy print ?
  1. <array name="sports">  
  2.         <item name="basketball">篮球item>  
  3.         <item name="football">足球item>  
  4.         <item name="baseball">棒球item>  
  5.         <item name="pingpang">乒乓球item>  
  6.     array>  

        篮球
        足球
        棒球
        乒乓球
    

         看下完整选项对话框的代码:

[java] view plain copy print ?
  1. new AlertDialog.Builder(Main.this)  
  2.                 .setTitle("标题")  
  3.                 .setItems(R.array.sports,  
  4.                         new DialogInterface.OnClickListener() {  
  5.                             @Override  
  6.                             public void onClick(DialogInterface dialog,  
  7.                                     int itemId) {  
  8.                                 //这里编写点击对话框按钮后触发的事件   
  9.                             }  
  10.                         }).show();  
new AlertDialog.Builder(Main.this)
				.setTitle("标题")
				.setItems(R.array.sports,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int itemId) {
								//这里编写点击对话框按钮后触发的事件
							}
						}).show();

        也是很简单吧,这里需要注意的是setItems可以换成其他形式,比如复选框setMultiChoiceItems等,自己动动手试试吧,最后在看个更简单的短暂显示提示框:Toast
[java] view plain copy print ?
  1. Toast.makeText(Main.this"就这么简单", Toast.LENGTH_SHORT).show();  
Toast.makeText(Main.this, "就这么简单", Toast.LENGTH_SHORT).show();

        第三个参数还可以选择Toast.LENGTH_LONG,显示的时间更加长一点。

       三种对话框结合起来写个简单示例:

[java] view plain copy print ?
  1. package com.dazlly;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.Toast;  
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.content.DialogInterface;  
  10.   
  11. public class Main extends Activity {  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.   
  18.         findViews();  
  19.   
  20.         setListener();  
  21.     }  
  22.   
  23.     private Button firstButton;  
  24.   
  25.     private void findViews() {  
  26.         firstButton = (Button) findViewById(R.id.firstButton);  
  27.     }  
  28.   
  29.     private void setListener() {  
  30.         firstButton.setOnClickListener(show);  
  31.     }  
  32.   
  33.     private Button.OnClickListener show = new Button.OnClickListener() {  
  34.         @Override  
  35.         public void onClick(View arg0) {  
  36.             new AlertDialog.Builder(Main.this)  
  37.                     .setTitle(R.string.title)  
  38.                     .setMessage(R.string.message)  
  39.                     .setNegativeButton(R.string.back,  
  40.                             new DialogInterface.OnClickListener() {  
  41.                                 @Override  
  42.                                 public void onClick(DialogInterface dialog,  
  43.                                         int which) {  
  44.                                 }  
  45.                             })  
  46.                     .setPositiveButton(R.string.go,  
  47.                             new DialogInterface.OnClickListener() {  
  48.                                 @Override  
  49.                                 public void onClick(DialogInterface dialog,  
  50.                                         int itemId) {  
  51.                                     showList();  
  52.                                 }  
  53.                             }).show();  
  54.         }  
  55.     };  
  56.   
  57.     private void showList() {  
  58.   
  59.         new AlertDialog.Builder(Main.this)  
  60.                 .setTitle(R.string.list)  
  61.                 .setItems(R.array.sports,  
  62.                         new DialogInterface.OnClickListener() {  
  63.                             @Override  
  64.                             public void onClick(DialogInterface dialog,  
  65.                                     int which) {  
  66.                                 String[] list = getResources().getStringArray(  
  67.                                         R.array.sports);  
  68.                                 Toast.makeText(Main.this"你选择了" + list[which],  
  69.                                         Toast.LENGTH_SHORT).show();  
  70.                             }  
  71.                         }).show();  
  72.     }  
  73. }  
package com.dazlly;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class Main extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViews();

		setListener();
	}

	private Button firstButton;

	private void findViews() {
		firstButton = (Button) findViewById(R.id.firstButton);
	}

	private void setListener() {
		firstButton.setOnClickListener(show);
	}

	private Button.OnClickListener show = new Button.OnClickListener() {
		@Override
		public void onClick(View arg0) {
			new AlertDialog.Builder(Main.this)
					.setTitle(R.string.title)
					.setMessage(R.string.message)
					.setNegativeButton(R.string.back,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
								}
							})
					.setPositiveButton(R.string.go,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int itemId) {
									showList();
								}
							}).show();
		}
	};

	private void showList() {

		new AlertDialog.Builder(Main.this)
				.setTitle(R.string.list)
				.setItems(R.array.sports,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								String[] list = getResources().getStringArray(
										R.array.sports);
								Toast.makeText(Main.this, "你选择了" + list[which],
										Toast.LENGTH_SHORT).show();
							}
						}).show();
	}
}

你可能感兴趣的:(Android_UI)