Android對話框Dialog詳解

Android對話框Dialog詳解
一個對話框一般是出現在當前Activity之上的小窗口,提示用戶做出決策或者輸入額外的信息,要求用戶採取行動才能繼續進行。下面這裏通過一個列子來顯示各種對話框,圖例:
複選對話框:
自定義對話框:

進度對話框:


main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示簡單對話框" />

    <Button
        android:id="@+id/button2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示列表對話框" />

    <Button
        android:id="@+id/button3"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示單選列表對話框" />

    <Button
        android:id="@+id/button4"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示覆選列表對話框" />

    <Button
        android:id="@+id/button5"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示自定義對話框" />

    <Button
        android:id="@+id/button6"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="顯示進度對話框" />

</RelativeLayout>

custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3366ff"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package cn.android.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn1, btn2, btn3, btn4, btn5, btn6;
	private final CharSequence[] items = { "北京", "上海", "廣東", "深圳" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn3 = (Button) findViewById(R.id.button3);
		btn4 = (Button) findViewById(R.id.button4);
		btn5 = (Button) findViewById(R.id.button5);
		btn6 = (Button) findViewById(R.id.button6);

		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.button1:// 顯示簡單對話框
			AlertDialog.Builder builder = new AlertDialog.Builder(
					MainActivity.this);
			builder.setTitle("提示")
					.setMessage("你確定要刪除嘛!")
					.setIcon(R.drawable.ic_launcher)
					.setPositiveButton("確定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

								}
							})
					.setNeutralButton("忽略",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

								}
							});
			AlertDialog alertDialog = builder.create();
			alertDialog.show();
			break;

		case R.id.button2:// 顯示列表對話框
			AlertDialog.Builder builder2 = new AlertDialog.Builder(
					MainActivity.this);
			builder2.setTitle("請選擇以下城市").setItems(items,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(MainActivity.this, items[which],
									Toast.LENGTH_LONG).show();
						}
					});
			AlertDialog alertDialog2 = builder2.create();
			alertDialog2.show();
			break;

		case R.id.button3:// 顯示單選列表對話框
			AlertDialog.Builder builder3 = new AlertDialog.Builder(
					MainActivity.this);
			builder3.setTitle("請選擇以下城市").setSingleChoiceItems(items, -1,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(MainActivity.this, items[which],
									Toast.LENGTH_LONG).show();
							dialog.dismiss();
						}
					});
			AlertDialog alertDialog3 = builder3.create();
			alertDialog3.show();
			break;

		case R.id.button4:// 顯示覆選列表對話框
			final boolean[] selected = { false, false, false, false };
			AlertDialog.Builder builder4 = new AlertDialog.Builder(
					MainActivity.this);
			builder4.setTitle("請選擇以下城市")
					.setMultiChoiceItems(items, selected,
							new DialogInterface.OnMultiChoiceClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which, boolean isChecked) {
									selected[which] = isChecked;
								}
							})
					.setPositiveButton("確定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									StringBuffer buffer = new StringBuffer();
									for (int i = 0; i < selected.length; i++) {
										if (selected[i] == true) {
											buffer.append(items[i]).append(",");
										}
									}
									if (!buffer.toString().trim().equals("")) {
										buffer.deleteCharAt(buffer.length() - 1);
										Toast.makeText(MainActivity.this,
												buffer.toString(),
												Toast.LENGTH_LONG).show();
									} else {
										Toast.makeText(MainActivity.this,
												"你沒有選擇任何城市!", Toast.LENGTH_LONG)
												.show();
									}
								}
							});
			AlertDialog alertDialog4 = builder4.create();
			alertDialog4.show();
			break;

		case R.id.button5:// 顯示自定義對話框
			Dialog dialog = new Dialog(MainActivity.this);
			View view = LayoutInflater.from(MainActivity.this).inflate(
					R.layout.custom, null);
			dialog.setContentView(view);
			dialog.setTitle("自定義對話框");
			TextView textView = (TextView) view.findViewById(R.id.text);
			ImageView imageView = (ImageView) view.findViewById(R.id.image);

			textView.setText("This is a customDialog!");
			imageView.setImageResource(R.drawable.ic_launcher);
			dialog.show();
			break;

		case R.id.button6:// 顯示進度對話框
			ProgressDialog progressDialog = new ProgressDialog(
					MainActivity.this);
			progressDialog.setTitle("下載提示");
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.show();
			break;
		}
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章