Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android對話框(一)AlertDialog

Android對話框(一)AlertDialog

編輯:關於Android編程

最近在做項目,小組幾個回了家。界面暫時沒人做,用到自定義對話框只能臨時去學。現在把對話框的相關整理。



    
package com.example.alertdialog;

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

public class MainActivity extends Activity {

	private Button button1;
	private Button button2;
	private final CharSequence[] items = {"廣州","深圳","上海","北京"}; 
	private Button button3;
	private Button button4;
	private Button button5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		button1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View view) {
				AlertDialog.Builder  builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("提示");
				builder.setMessage("你確定要刪除嗎!");
				builder.setIcon(R.drawable.ic_launcher);
				builder.setPositiveButton("確定", new  DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int arg1) {
						// 讓對話框消失
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						
					}
				});
				builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						//忽略用戶操作,作用和取消一樣
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("提示");
				//不能設置setMessage的內容,不然會覆蓋原來item的值
				//在OnClickListener前要加DialogInterface,不然會報錯
				builder.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int index) {
						//item[index]不能轉化為String,轉化為CharSequence就行了
						CharSequence select_item = items[index];
						Toast.makeText(MainActivity.this, "你選擇了城市"+select_item, 1).show();
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("請選擇以下城市");
				builder.setIcon(R.drawable.ic_launcher);
				/*
				 * 第一個參數:顯示在對話框中供選擇列表
				 * 第二個參數:默認選擇的值,-1表示一個都沒選
				 * 第三個參數:監聽器
				 */
	            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						CharSequence select_item = items[which];
						Toast.makeText(MainActivity.this, "-->>"+select_item, 1).show();
						dialog.dismiss();
					}
				});
				AlertDialog alertDialog = builder.create();
	            alertDialog.show();
			}
		});
		button4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("請選擇以下城市");
				/*
				 * 第一個參數:供選擇多選列表
				 * 第二個參數:boolean類型的數組,表示選中的項;如設置為null,表示沒有選項默認被選中
				 * 第三個參數:不用說,監聽器
				 */
				builder.setMultiChoiceItems(items, new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1, boolean ischecked) {
						// TODO Auto-generated method stub
					}
				});
				builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// 直接獲取選擇的值
						
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		//其實是button4的另一種寫法
		button5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("請選擇以下城市");
				//獲取選擇值:使用StringBuffer追加,中間用逗號分隔開來
				builder.setMultiChoiceItems(R.array.city,  new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
					public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
						// TODO Auto-generated method stub
					}
				});
				builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						// 獲取選擇的值
						
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
	}
}
\\\\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved