AlertDialog和AlertDialog.Builder兩者區別
那我們先了解什麼是AlertDialog?什麼是AlertDialog.Builder?且兩者有什麼區別?
AlertDialog是Dialog的一個直接子類,AlertDialog也是Android系統當中最常用的對話方塊之一。
一個AlertDialog可以有兩個以上的Button,可以對一個AlertDialog設定相應的資訊。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。
但不能直接通過AlertDialog的建構函式來生產一個AlertDialog。研究AlertDialog的原始碼發現AlertDialog所有的構造方法都是防寫的所以不能通過:AlertDialog alertDialog = new AlertDialog();來得到。AlertDialog構造方法原始碼如下:
- protected AlertDialog(Context context) {
- this(context, com.android.internal.R.style.Theme_Dialog_Alert);
- }
- protected AlertDialog(Context context, int theme) {
- super(context, theme);
- mAlert = new AlertController(context, this, getWindow());
- }
- protected AlertDialog(Context context,
- super(context, com.android.internal.R.style.Theme_Dialog_Alert);
- setCancelable(cancelable);
- setOnCancelListener(cancelListener);
- mAlert = new AlertController(context, this, getWindow());
- }
只能通過:
- AlertDialog.Builder alertDialog =
來得到。
那就通過一個具體的例項來說說吧(這裡用一個最常用的對話方塊為例):
- package com.oyah;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- publicclass TestsActivity extends Activity {
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
- alertDialog
- .setTitle(”title”)
- .setMessage(”message”)
- .setPositiveButton(”okBuuon”,
- new DialogInterface.OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog,
- int which) {
- }
- })
- .setNegativeButton(”exitButton”,
- new DialogInterface.OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog,
- int which) {
- }
- }).[b]setCancelable(false).[/b]create().show();
- }
- }
針對AlertDialog中設定了確定和取消按鈕,一般來說確定為執行某個動作,取消就是不執行,但是如果使用者點選了系統的Back鍵,此時就會將AlertDialog關閉,而並沒有執行預期的取消的操作。
此時需要關注一個方法setCancelable(false) 該方法定義設定該AlertDialog是否可以被Back鍵取消,如果不設定預設為true
下面是一些擴充套件:
根據AlertDialog.Builder 建立 相應的 AlertDialog
- AlertDialog alertDialogs = alertDialog.create();
用dismiss();方法來清除建立過的AlertDialog
- alertDialogs.dismiss();
以上所採用的都是AlertDialog 系統預設的佈局 現在說自定義佈局的情況 並新增一個用於取消AlertDialog 的 Button
定義其佈局 main.xml
- <?xml version=“1.0” encoding=“utf-8”?>
- <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
- android:orientation=”horizontal”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- android:padding=”10dp”
- >
- <LinearLayout
- android:orientation=”vertical”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- >
- </LinearLayout>
- </LinearLayout>
通過LayoutInflater 得到上面 main.xml 佈局檔案
- view = this.getLayoutInflater().inflate(R.layout.main, null);
指定AlertDialog.Builder 所需的佈局 並返回目標AlertDialog
- alertDialog.setView(view);
- alertDialogs= alertDialog.create();
通過 view.findViewById() 得到 目標View 然後設定其內容 如:
- TextView title = (TextView) view.findViewById(R.id.title);
- title.setTextSize(20);
- title.setTextColor(Color.RED);
- title.setText(”Title”);
- TextView message = (TextView) view.findViewById(R.id.message);
- message.setText(”message”);
顯示對話方塊 AlertDialog
- findViewById(R.id.button).setOnClickListener(new OnClickListener(){
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- alertDialogs.show();
- }
- });
清除對話方塊 AlertDialog
- view.setOnClickListener(new OnClickListener(){
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- alertDialogs.dismiss();
- }
- });
</td>
<script>
var html = document.getElementById("artContent").innerHTML;
document.getElementById("artContent").innerHTML = html;
</script>
</tr>
</tbody>
</table>
<div id="viewerPlaceHolder" style="width: 717px; height: 700px; display: none; margin: 0 auto;">
</div>