1. 程式人生 > >Android中AlterDialog四種實現樣式

Android中AlterDialog四種實現樣式

   (1)用於提示資訊  如:是否退出系統?是 OR  否

    第一步:例項化AlertDialog.Builder物件

    AlertDialog.Builder    builder=new AlertDialog.Builder(this);

    ps:AlertDialog的構造方法是protected,所以無法例項化,通過Builder完成操作。

    Builder是AlertDialog的一個靜態內部類。

    第二步:通過Builder物件,設定對話方塊相關資訊(標題,內容,按鈕)

    按鈕有三個PositiveButton(積極的)、NegativeButton(消極的)、NeutralButton(中立的)

    三個按鈕  首先展示NegativeButton,接著展示NeutralButton,最後展示PositiveButton。

    ps:setCancelable(false)   //禁止點選對話方塊之外的區域  取消或關閉   也禁止了返回鍵


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

public class AlertDialogActivity extends Activity {
	
	AlertDialog.Builder alert;
	long beginTime = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initDialog();
		
	}
	private void initDialog(){
		alert = new AlertDialog.Builder(this);
		alert.setTitle("溫馨提示").setIcon(R.drawable.ic_launcher).setMessage("是否確定退出").setPositiveButton("確定",
				new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						finish();
					}
				}).setNegativeButton("取消", null).setNeutralButton("等一會", null);
		alert.create();
		alert.show();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	public void exit() {
		//實現兩次點選退出系統;
		if (System.currentTimeMillis() - beginTime > 2000) {
			Toast.makeText(this, "再點選一次退出系統", Toast.LENGTH_SHORT).show();
			beginTime = System.currentTimeMillis();
		}else {
			finish();
			System.exit(0);
		}
		
		
	}
}
    (2)AlertDialog除了有資訊提示的作用外,還有展示列表的功能

   <1> setItems   設定普通列表


     <2> setSingleChoiceItems設定單選按鈕列表


     <3> setMultiChoiceItems設定多選複選列表


<4>    還可以通過 setView 新增一個自定義佈局,實現編輯資訊的目的;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AlertDialogToolsActivity extends Activity {
	TextView textView;
	AlertDialog.Builder alert;
	long beginTime = 0;
	private int defaultItem = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alter_tools);
		initView();	
	}

	
	private void initView() {
		textView = (TextView) findViewById(R.id.text_popup);
	}
	//在xml檔案中繫結click,可以簡化java程式碼,不用例項化控制元件,可以直接通過控制元件ID進行監聽;
	public void click(View view) {
		switch (view.getId()) {
		case R.id.button_textcolor:
			showCommonListDialog();
			break;
		case R.id.button_textsize:
			showSingleListDialog();
			break;
		case R.id.button_textcontent:
			showMultiListDialog();
			break;

		default:
			break;
		}
	}
<strong>//<span style="color:#ff0000;">設定多選複選列表</span></strong>
	private void showMultiListDialog() {
		// TODO Auto-generated method stub
		alert = new AlertDialog.Builder(this);
		alert.setIcon(R.drawable.ic_launcher);
		alert.setTitle("請選擇個人愛好:");
		//複選樣式Item:需要設定顯示的資料,需要監聽選擇的Item狀態的初始化(方便得到指定的被選擇的資訊文字並處理),
		//監聽Item的選擇事件(方便不同型別資訊的獲取);
		final String[] hobits = getResources().getStringArray(R.array.text_size_hobit);
		final boolean[] checkedItems = new boolean[hobits.length];
		alert.setMultiChoiceItems(R.array.text_size_hobit, checkedItems, new OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
				checkedItems[which] = isChecked;
			}
		}).setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				textView.setText("您選擇的愛好是:\n");
				for (int i = 0; i < checkedItems.length; i++) {
					if (checkedItems[i]) {
						textView.append(hobits[i]+"\n");
					}
				}
			}
		}).create();
		alert.show();
	}

//<strong><span style="color:#ff0000;">設定單選按鈕列表</span></strong>
	private void showSingleListDialog() {
		alert = new AlertDialog.Builder(this);
		alert.setIcon(R.drawable.ic_launcher);
		alert.setTitle("設定字型大小");
		//單選樣式的Item:需要傳遞的內容有,顯示內容(陣列),和設定點選的Item的位置Position,設定每個Item的點選監聽;
		alert.setSingleChoiceItems(R.array.text_size_name, defaultItem, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				defaultItem = which;
				textView.setTextSize(getResources().getIntArray(R.array.text_size)[which]);
				dialog.dismiss();
			}
		}).create();
		alert.show();
	}

//<span style="font-weight: bold;"><span style="color:#ff0000;">設定普通列表</span></span>
	private void showCommonListDialog() {
		alert = new AlertDialog.Builder(this);
		alert.setIcon(R.drawable.ic_launcher);
		alert.setTitle("設定字型顏色");
		//設定普通文字格式的對話方塊,設定的是普通的Item;
		alert.setItems(R.array.text_color, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					textView.setTextColor(Color.RED);
					break;
				case 1:
					textView.setTextColor(Color.BLUE);
					break;
				case 2:
					textView.setTextColor(Color.GREEN);
					break;

				default:
					break;
				}
			}
		}).create();
		alert.show();
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	public void exit() {
		//實現兩次點選退出系統;
		if (System.currentTimeMillis() - beginTime > 2000) {
			Toast.makeText(this, "再點選一次退出系統", Toast.LENGTH_SHORT).show();
			beginTime = System.currentTimeMillis();
		}else {
			finish();
			System.exit(0);
		}
		
		
	}
}<strong>
</strong>