Android使用AlertDialog創(chuàng)建對話框
AlertDialog類的功能十分強(qiáng)大,它不僅可以生成帶按鈕的提示對話框,還可以生成帶列表的列表對話框,概括起來有一下4種:
1.帶確定、中立和取消等N個按鈕的提示對話框,其中的按鈕個數(shù)不是固定的,可以根據(jù)需要添加。例如,不需要中立按鈕,則可以只生成帶有確定和取消按鈕的對話框,也可以是只需有一個按鈕的對話框。
2.帶列表的列表對話框
3.帶多個單選列表項和N個按鈕的列表對話框
4.帶多個多選列表項和N個按鈕的列表對話框
在使用AlertDialog類生成對話框時,常用的方法如下所示:
setTitle :為對話框設(shè)置標(biāo)題
setIcon :為對話框設(shè)置圖標(biāo)
setMessage:為對話框設(shè)置內(nèi)容
setButton:用于為提示對話框添加按鈕,可以是取消按鈕、中立按鈕和確定按鈕。需要通過為其指定int類型的whichButton參數(shù)實現(xiàn),其參數(shù)可以是DialogInterface.BUTTON_POSITIVE(確定按鈕)、BUTTON_NEGATIVE(取消按鈕)或者BUTTON_NEUTRAL(中立按鈕)。
通常情況下,使用AlertDialog類只能生成帶N個按鈕的提示對話框,要生成另外3種列表對話框,需要使用AlertDialog.Builder類,AlertDialog.Builder類提供的常用方法如下表:
setTitle :為對話框設(shè)置標(biāo)題
setIcon :為對話框設(shè)置圖標(biāo)
setMessage:為對話框設(shè)置內(nèi)容
setView : 給對話框設(shè)置自定義樣式
setItems :設(shè)置對話框要顯示的一個list,一般用于顯示幾個命令時
setSingleChoiceItems :用來設(shè)置對話框顯示一系列的單選框
setMultiChoiceItems :用來設(shè)置對話框顯示一系列的復(fù)選框
setNeutralButton :普通按鈕
setPositiveButton :給對話框添加"Yes"按鈕
setNegativeButton :對話框添加"No"按鈕
create : 創(chuàng)建對話框
show :顯示對話框
下面通過一個具體的實例說明如何使用AlertDialog類生成提示對話框和各種列表對話框
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout1" android:gravity="center_horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示帶取消、中立和確定按鈕的對話框"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示列表的對話框"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示帶單選列表對話框"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示帶多選列表對話框"/> </LinearLayout>
界面為圖
MainActivity:
package com.example.test; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private boolean[] checkedItems;//記錄各個列表項的狀態(tài) private String[] items;//各列表項要顯示的內(nèi)容 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //顯示帶取消、中立和確定按鈕的對話框 Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create(); alert.setIcon(R.drawable.ic_launcher);//設(shè)置對話框的圖標(biāo) alert.setTitle("系統(tǒng)提示");//設(shè)置對話框的標(biāo)題 alert.setMessage("顯示帶取消、中立和確定按鈕的對話框!");//設(shè)置對話框顯示的內(nèi)容 //添加“取消”按鈕 alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { Toast.makeText(MainActivity.this, "您單擊了取消按鈕", Toast.LENGTH_SHORT).show(); } }); //添加“確定”按鈕 alert.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { Toast.makeText(MainActivity.this, "您單擊了確定按鈕", Toast.LENGTH_SHORT).show(); } }); //添加“中立”按鈕 alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { Toast.makeText(MainActivity.this, "您單擊了中立按鈕", Toast.LENGTH_SHORT).show(); } }); alert.show();//顯示對話框 } }); //顯示列表的對話框 Button button2=(Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { final String[] items=new String[]{"唱歌","跳舞","美術(shù)","遠(yuǎn)足旅行","攝影"}; Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.in); builder.setTitle("請選擇你的愛好:"); //添加列表項 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您選擇了"+items[which],Toast.LENGTH_SHORT).show(); } }); builder.create().show();//創(chuàng)建對話框并顯示 } }); //顯示帶單選列表對話框 Button button3=(Button)findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { final String[] items=new String[]{"標(biāo)準(zhǔn)","無聲","會議","戶外","離線"}; Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.music); builder.setTitle("請選擇要使用的情景模式:"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您選擇了"+items[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確定", null); builder.create().show();//創(chuàng)建對話框并顯示 } }); //顯示帶多選列表對話框 Button button4=(Button)findViewById(R.id.button4); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { checkedItems =new boolean[]{false,true,false,true,false};//記錄各列表的狀態(tài) //各列表項要顯示的內(nèi)容 items=new String[]{"植物大戰(zhàn)僵尸","憤怒的小鳥","泡泡龍","開心消消樂","地鐵跑酷"}; //顯示帶單選列表框的對話框 Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.setting); builder.setTitle("請選擇您喜歡的游戲:"); builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which]=isChecked; } }); //為對話框添加"確定"按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result=""; for (int i = 0; i <checkedItems.length; i++) { if(checkedItems[i]){ result+=items[i]+"、"; } } //當(dāng)result不為空時,通過消息提示框顯示選擇的結(jié)果 if(!"".equals(result)){ result=result.substring(0,result.length()-1);//去掉最后的"、"號 Toast.makeText(MainActivity.this, "您選擇了:["+result+"]",Toast.LENGTH_SHORT).show(); } } }); builder.create().show();//創(chuàng)建對話框并顯示 } }); } }
效果如圖:
點擊第一個按鈕:
點擊第二個按鈕:
點擊第三個按鈕:
點擊第四個按鈕:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android控件AppWidgetProvider使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android控件AppWidgetProvider的使用方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android 采用AOP方式封裝6.0權(quán)限管理的方法
這篇文章主要介紹了Android 采用AOP方式封裝6.0權(quán)限管理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Android實現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法
這篇文章主要介紹了Android實現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法,涉及Android中Activities的使用技巧,需要的朋友可以參考下2015-04-04Android 取消藍(lán)牙配對框?qū)崿F(xiàn)自動配對功能
這篇文章主要介紹了Android 取消藍(lán)牙配對框?qū)崿F(xiàn)自動配對功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02解決android設(shè)備斷電重啟后WIFI不能自動重連的BUG(收藏)
這篇文章主要介紹了解決android設(shè)備斷電重啟后WIFI不能自動重連的BUG,本文給出了問題描述及分析過程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11