主頁(yè) > 知識(shí)庫(kù) > Android的AlertDialog詳解

Android的AlertDialog詳解

熱門(mén)標(biāo)簽:蘭州智能語(yǔ)音電銷機(jī)器人功能 江蘇客服外呼系統(tǒng)排名 外呼crm系統(tǒng)是什么 東莞小型外呼系統(tǒng)招商 360地圖標(biāo)注位置 蘇州園區(qū)地圖標(biāo)注 宜賓穩(wěn)定外呼系統(tǒng)廠家 百度地圖標(biāo)注中心api 興隆縣地圖標(biāo)注app
AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過(guò)new一個(gè)AlertDialog來(lái)創(chuàng)建出一個(gè)AlertDialog。
要?jiǎng)?chuàng)建一個(gè)AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創(chuàng)建對(duì)話框需要了解以下幾個(gè)方法:
setTitle :為對(duì)話框設(shè)置標(biāo)題
setIcon :為對(duì)話框設(shè)置圖標(biāo)
setMessage:為對(duì)話框設(shè)置內(nèi)容
setView : 給對(duì)話框設(shè)置自定義樣式
setItems :設(shè)置對(duì)話框要顯示的一個(gè)list,一般用于顯示幾個(gè)命令時(shí)
setMultiChoiceItems :用來(lái)設(shè)置對(duì)話框顯示一系列的復(fù)選框
setNeutralButton    :普通按鈕
setPositiveButton   :給對(duì)話框添加"Yes"按鈕
setNegativeButton :對(duì)話框添加"No"按鈕
create : 創(chuàng)建對(duì)話框
show :顯示對(duì)話框
一、簡(jiǎn)單的AlertDialog
下面,創(chuàng)建一個(gè)簡(jiǎn)單的ALertDialog并顯示它:

[java]  package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("對(duì)話框的標(biāo)題"). 
                setMessage("對(duì)話框的內(nèi)容"). 
                setIcon(R.drawable.ic_launcher). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("對(duì)話框的標(biāo)題").
    setMessage("對(duì)話框的內(nèi)容").
    setIcon(R.drawable.ic_launcher).
    create();
  alertDialog.show();
 }
}運(yùn)行結(jié)果如下:
 
 

二、帶按鈕的AlertDialog
上面的例子很簡(jiǎn)單,下面我們?cè)谶@個(gè)AlertDialog上面加幾個(gè)Button,實(shí)現(xiàn)刪除操作的提示對(duì)話框

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("確定刪除?"). 
                setMessage("您確定刪除該條信息嗎?"). 
                setIcon(R.drawable.ic_launcher). 
                setPositiveButton("確定", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                setNeutralButton("查看詳情", new DialogInterface.OnClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("確定刪除?").
    setMessage("您確定刪除該條信息嗎?").
    setIcon(R.drawable.ic_launcher).
    setPositiveButton("確定", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}在這個(gè)例子中,我們定義了三個(gè)按鈕,分別是"Yes"按鈕,"No"按鈕以及一個(gè)普通按鈕,每個(gè)按鈕都有onClick事件,TODO的地方可以放點(diǎn)了按鈕之后想要做的一些處理
看一下運(yùn)行結(jié)果:
 

可以看到三個(gè)按鈕添加到了AlertDialog上,三個(gè)沒(méi)有添加事件處理的按鈕,點(diǎn)了只是關(guān)閉對(duì)話框,沒(méi)有任何其他操作。
 
 
 
三、類似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似ListView的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" }; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜歡吃哪種水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setItems(arrayFruit, new DialogInterface.OnClickListener() { 
  
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" };
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜歡吃哪種水果?").
    setIcon(R.drawable.ic_launcher)
    .setItems(arrayFruit, new DialogInterface.OnClickListener() {
 
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}運(yùn)行結(jié)果如下:
 
 
 
 
四、類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似RadioButton的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是初始值(初始被選中的item),第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
在這個(gè)例子里面我們?cè)O(shè)了一個(gè)selectedFruitIndex用來(lái)記住選中的item的index

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
     
    private int selectedFruitIndex = 0; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" }; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜歡吃哪種水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() { 
  
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        selectedFruitIndex = which; 
                    } 
                }). 
                setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 
 private int selectedFruitIndex = 0;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" };
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜歡吃哪種水果?").
    setIcon(R.drawable.ic_launcher)
    .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
 
     @Override
     public void onClick(DialogInterface dialog, int which) {
      selectedFruitIndex = which;
     }
    }).
    setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}
運(yùn)行結(jié)果如下:
 
 

五、類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法來(lái)實(shí)現(xiàn)類似CheckBox的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是選中狀態(tài)的數(shù)組,第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件

[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" }; 
        final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false}; 
 
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("你喜歡吃哪種水果?"). 
                setIcon(R.drawable.ic_launcher) 
                .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() { 
                     
                    @Override 
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
                        arrayFruitSelected[which] = isChecked; 
                    } 
                }). 
                setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        StringBuilder stringBuilder = new StringBuilder(); 
                        for (int i = 0; i arrayFruitSelected.length; i++) { 
                            if (arrayFruitSelected[i] == true) 
                            { 
                                stringBuilder.append(arrayFruit[i] + "、"); 
                            } 
                        } 
                        Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); 
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final String[] arrayFruit = new String[] { "蘋(píng)果", "橘子", "草莓", "香蕉" };
  final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("你喜歡吃哪種水果?").
    setIcon(R.drawable.ic_launcher)
    .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
      arrayFruitSelected[which] = isChecked;
     }
    }).
    setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      StringBuilder stringBuilder = new StringBuilder();
      for (int i = 0; i arrayFruitSelected.length; i++) {
       if (arrayFruitSelected[i] == true)
       {
        stringBuilder.append(arrayFruit[i] + "、");
       }
      }
      Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}運(yùn)行結(jié)果如下:
 
 
 
六、自定義View的AlertDialog
有時(shí)候我們不能滿足系統(tǒng)自帶的AlertDialog風(fēng)格,就比如說(shuō)我們要實(shí)現(xiàn)一個(gè)Login畫(huà)面,有用戶名和密碼,這時(shí)我們就要用到自定義View的AlertDialog
先創(chuàng)建Login畫(huà)面的布局文件
[html] ?xml version="1.0" encoding="utf-8"?> 
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center" > 
 
        TextView 
            android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:text="@string/user" /> 
 
        EditText 
            android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" /> 
    /LinearLayout> 
 
    LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center" > 
 
        TextView 
            android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:text="@string/passward" /> 
 
        EditText 
            android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" /> 
    /LinearLayout> 
 
/LinearLayout> 
?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >
        TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/user" />
        EditText
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    /LinearLayout>
    LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >
        TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/passward" />
        EditText
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    /LinearLayout>
/LinearLayout>
然后在Activity里面把Login畫(huà)面的布局文件添加到AlertDialog上
[java] package com.tianjf; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
 
public class Dialog_AlertDialogDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 取得自定義View  
        LayoutInflater layoutInflater = LayoutInflater.from(this); 
        View myLoginView = layoutInflater.inflate(R.layout.login, null); 
         
        Dialog alertDialog = new AlertDialog.Builder(this). 
                setTitle("用戶登錄"). 
                setIcon(R.drawable.ic_launcher). 
                setView(myLoginView). 
                setPositiveButton("登錄", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                }). 
                create(); 
        alertDialog.show(); 
    } 

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 取得自定義View
  LayoutInflater layoutInflater = LayoutInflater.from(this);
  View myLoginView = layoutInflater.inflate(R.layout.login, null);
  
  Dialog alertDialog = new AlertDialog.Builder(this).
    setTitle("用戶登錄").
    setIcon(R.drawable.ic_launcher).
    setView(myLoginView).
    setPositiveButton("登錄", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    setNegativeButton("取消", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
     }
    }).
    create();
  alertDialog.show();
 }
}運(yùn)行結(jié)果如下:
 
摘自 殤雲(yún)的專欄

標(biāo)簽:嘉興 無(wú)錫 蘇州 四川 朝陽(yáng) 烏魯木齊 雞西 商洛

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Android的AlertDialog詳解》,本文關(guān)鍵詞  Android,的,AlertDialog,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Android的AlertDialog詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Android的AlertDialog詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章