8種android 對(duì)話框(Dialog)使用方法詳解
本文匯總了android 8種對(duì)話框(Dialog)使用方法,分享給大家供大家參考,具體內(nèi)容如下
1.寫(xiě)在前面
Android提供了豐富的Dialog函數(shù),本文介紹最常用的8種對(duì)話框的使用方法,包括普通(包含提示消息和按鈕)、列表、單選、多選、等待、進(jìn)度條、編輯、自定義等多種形式,將在第2部分介紹。
有時(shí),我們希望在對(duì)話框創(chuàng)建或關(guān)閉時(shí)完成一些特定的功能,這需要復(fù)寫(xiě)Dialog的create()、show()、dismiss()等方法,將在第3部分介紹。
2.代碼示例
2.1 普通Dialog(圖1與圖2)
2個(gè)按鈕
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
}
private void showNormalDialog(){
/* @setIcon 設(shè)置對(duì)話框圖標(biāo)
* @setTitle 設(shè)置對(duì)話框標(biāo)題
* @setMessage 設(shè)置對(duì)話框消息提示
* setXXX方法返回Dialog對(duì)象,因此可以鏈?zhǔn)皆O(shè)置屬性
*/
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一個(gè)普通Dialog")
normalDialog.setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
normalDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton("關(guān)閉",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 顯示
normalDialog.show();
}
}
3個(gè)按鈕
/* @setNeutralButton 設(shè)置中間的按鈕
* 若只需一個(gè)按鈕,僅設(shè)置 setPositiveButton 即可
*/
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一個(gè)普通Dialog").setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
normalDialog.setPositiveButton("按鈕1",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNeutralButton("按鈕2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNegativeButton("按鈕3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
// 創(chuàng)建實(shí)例并顯示
normalDialog.show();
}
2.2 列表Dialog(圖3)
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("我是一個(gè)列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which 下標(biāo)從0開(kāi)始
// ...To-do
Toast.makeText(MainActivity.this,
"你點(diǎn)擊了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
2.3 單選Dialog(圖4)
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1","我是2","我是3","我是4" };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一個(gè)單選Dialog");
// 第二個(gè)參數(shù)是默認(rèn)選項(xiàng),此處設(shè)置為0
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
"你選擇了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
2.4 多選Dialog(圖5)
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
// 設(shè)置默認(rèn)選中的選項(xiàng),全為false默認(rèn)均未選中
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle("我是一個(gè)多選Dialog");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += items[yourChoices.get(i)] + " ";
}
Toast.makeText(MainActivity.this,
"你選中了" + str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}
2.5 等待Dialog(圖6)
private void showWaitingDialog() {
/* 等待Dialog具有屏蔽其他控件的交互能力
* @setCancelable 為使屏幕不可點(diǎn)擊,設(shè)置為不可取消(false)
* 下載等事件完成后,主動(dòng)調(diào)用函數(shù)關(guān)閉該Dialog
*/
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("我是一個(gè)等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}
2.6 進(jìn)度條Dialog(圖7)
private void showProgressDialog() {
/* @setProgress 設(shè)置初始進(jìn)度
* @setProgressStyle 設(shè)置樣式(水平進(jìn)度條)
* @setMax 設(shè)置進(jìn)度最大值
*/
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("我是一個(gè)進(jìn)度條Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模擬進(jìn)度增加的過(guò)程
* 新開(kāi)一個(gè)線程,每個(gè)100ms,進(jìn)度增加1
*/
new Thread(new Runnable() {
@Override
public void run() {
int progress= 0;
while (progress < MAX_PROGRESS){
try {
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
}
}
// 進(jìn)度達(dá)到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
}
2.7 編輯Dialog(圖8)
private void showInputDialog() {
/*@setView 裝入一個(gè)EditView
*/
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle("我是一個(gè)輸入Dialog").setView(editText);
inputDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}
2.8 自定義Dialog(圖9)
<!-- res/layout/dialog_customize.xml-->
<!-- 自定義View -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
private void showCustomizeDialog() {
/* @setView 裝入自定義View ==> R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一個(gè)EditView,因此和圖8一樣
* dialog_customize.xml可自定義更復(fù)雜的View
*/
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this);
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle("我是一個(gè)自定義Dialog");
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 獲取EditView中的輸入內(nèi)容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}
3.復(fù)寫(xiě)回調(diào)函數(shù)
/* 復(fù)寫(xiě)B(tài)uilder的create和show函數(shù),可以在Dialog顯示前實(shí)現(xiàn)必要設(shè)置
* 例如初始化列表、默認(rèn)選項(xiàng)等
* @create 第一次創(chuàng)建時(shí)調(diào)用
* @show 每次顯示時(shí)調(diào)用
*/
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this){
@Override
public AlertDialog create() {
items[0] = "我是No.1";
return super.create();
}
@Override
public AlertDialog show() {
items[1] = "我是No.2";
return super.show();
}
};
listDialog.setTitle("我是一個(gè)列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
/* @setOnDismissListener Dialog銷毀時(shí)調(diào)用
* @setOnCancelListener Dialog關(guān)閉時(shí)調(diào)用
*/
listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
"Dialog被銷毀了",
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問(wèn)題
- 屬于自己的Android對(duì)話框(Dialog)自定義集合
- Android自定義Dialog實(shí)現(xiàn)通用圓角對(duì)話框
相關(guān)文章
Flutter框架解決盒約束widget和assets里加載資產(chǎn)技術(shù)
這篇文章主要為大家介紹了Flutter框架解決盒約束widget和assets里加載資產(chǎn)技術(shù)運(yùn)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
分享Android開(kāi)發(fā)自學(xué)筆記之AndroidStudio常用功能
這篇文章主要給大家分享Android開(kāi)發(fā)自學(xué)筆記之AndroidStudio常用功能的相關(guān)資料,需要的朋友可以參考下2015-12-12
懸浮對(duì)話框Android代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了懸浮對(duì)話框Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框(二)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了Android編程之九宮格實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android九宮格的實(shí)現(xiàn)方法與具體步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01
Android如何使用圓形揭露動(dòng)畫(huà)巧妙地隱藏或顯示View詳解
Android開(kāi)發(fā)中會(huì)遇到不少顯示和隱藏的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Android如何使用圓形揭露動(dòng)畫(huà)巧妙地隱藏或顯示View的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Android 高仿微信轉(zhuǎn)賬金錢(qián)輸入框規(guī)則
這篇文章主要介紹了Android 高仿微信金錢(qián)輸入框規(guī)則的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
快速解決fragment中onActivityResult不調(diào)用的問(wèn)題
下面小編就為大家?guī)?lái)一篇快速解決fragment中onActivityResult不調(diào)用的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼
這篇文章主要介紹了Android Filterable實(shí)現(xiàn)Recyclerview篩選功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

