android 對話框

android 8種對話框(Dialog)使用方法匯總

作者:@gzdaijie
本文為作者原創,轉載請注明出處:https://www.cnblogs.com/gzdaijie/p/5222191.html

目錄

1.寫在前面
2.代碼示例
2.1 普通Dialog(圖1與圖2)
2.2 列表Dialog(圖3)
2.3 單選Dialog(圖4)
2.4 多選Dialog(圖5)
2.5 等待Dialog(圖6)
2.6 進度條Dialog(圖7)
2.7 編輯Dialog(圖8)
2.8 自定義Dialog(圖9)
3.復寫回調函數

博客逐步遷移至 呆兔兔的小站

1.寫在前面

  • Android提供了豐富的Dialog函數,本文介紹最常用的8種對話框的使用方法,包括普通(包含提示消息和按鈕)、列表、單選、多選、等待、進度條、編輯、自定義等多種形式,將在第2部分介紹。
  • 有時,我們希望在對話框創建或關閉時完成一些特定的功能,這需要復寫Dialog的create()show()dismiss()等方法,將在第3部分介紹。

2.代碼示例

圖片示例

2.1 普通Dialog(圖1與圖2)

  • 2個按鈕
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 設置對話框圖標
?????????* @setTitle 設置對話框標題
?????????* @setMessage 設置對話框消息提示
?????????* setXXX方法返回Dialog對象,因此可以鏈式設置屬性
?????????*/
????????final AlertDialog.Builder normalDialog =
????????????new AlertDialog.Builder(MainActivity.this);
????????normalDialog.setIcon(R.drawable.icon_dialog);
????????normalDialog.setTitle("我是一個普通Dialog")
????????normalDialog.setMessage("你要點擊哪一個按鈕呢?");
????????normalDialog.setPositiveButton("確定",
????????????new DialogInterface.OnClickListener() {
????????????@Override
????????????public void onClick(DialogInterface dialog, int which) {
????????????????//...To-do
????????????}
????????});
????????normalDialog.setNegativeButton("關閉",
????????????new DialogInterface.OnClickListener() {
????????????@Override
????????????public void onClick(DialogInterface dialog, int which) {
????????????????//...To-do
????????????}
????????});
????????// 顯示
????????normalDialog.show();
????}
}
  • 3個按鈕
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* @setNeutralButton 設置中間的按鈕
?* 若只需一個按鈕,僅設置 setPositiveButton 即可
?*/
private void showMultiBtnDialog(){
????AlertDialog.Builder normalDialog =
????????new AlertDialog.Builder(MainActivity.this);
????normalDialog.setIcon(R.drawable.icon_dialog);
????normalDialog.setTitle("我是一個普通Dialog").setMessage("你要點擊哪一個按鈕呢?");
????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
????????}
????});
????// 創建實例并顯示
????normalDialog.show();
}

2.2 列表Dialog(圖3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void showListDialog() {
????final String[] items = { "我是1","我是2","我是3","我是4" };
????AlertDialog.Builder listDialog =
????????new AlertDialog.Builder(MainActivity.this);
????listDialog.setTitle("我是一個列表Dialog");
????listDialog.setItems(items, new DialogInterface.OnClickListener() {
????????@Override
????????public void onClick(DialogInterface dialog, int which) {
????????????// which 下標從0開始
????????????// ...To-do
????????????Toast.makeText(MainActivity.this,
????????????????"你點擊了" + items[which],
????????????????Toast.LENGTH_SHORT).show();
????????}
????});
????listDialog.show();
}

2.3 單選Dialog(圖4)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int yourChoice;
private void showSingleChoiceDialog(){
????final String[] items = { "我是1","我是2","我是3","我是4" };
????yourChoice = -1;
????AlertDialog.Builder singleChoiceDialog =
????????new AlertDialog.Builder(MainActivity.this);
????singleChoiceDialog.setTitle("我是一個單選Dialog");
????// 第二個參數是默認選項,此處設置為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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
????final String[] items = { "我是1","我是2","我是3","我是4" };
????// 設置默認選中的選項,全為false默認均未選中
????final boolean initChoiceSets[]={false,false,false,false};
????yourChoices.clear();
????AlertDialog.Builder multiChoiceDialog =
????????new AlertDialog.Builder(MainActivity.this);
????multiChoiceDialog.setTitle("我是一個多選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)

1
2
3
4
5
6
7
8
9
10
11
12
13
private void showWaitingDialog() {
????/* 等待Dialog具有屏蔽其他控件的交互能力
?????* @setCancelable 為使屏幕不可點擊,設置為不可取消(false)
?????* 下載等事件完成后,主動調用函數關閉該Dialog
?????*/
????ProgressDialog waitingDialog=
????????new ProgressDialog(MainActivity.this);
????waitingDialog.setTitle("我是一個等待Dialog");
????waitingDialog.setMessage("等待中...");
????waitingDialog.setIndeterminate(true);
????waitingDialog.setCancelable(false);
????waitingDialog.show();
}

2.6 進度條Dialog(圖7)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private void showProgressDialog() {
????/* @setProgress 設置初始進度
?????* @setProgressStyle 設置樣式(水平進度條)
?????* @setMax 設置進度最大值
?????*/
????final int MAX_PROGRESS = 100;
????final ProgressDialog progressDialog =
????????new ProgressDialog(MainActivity.this);
????progressDialog.setProgress(0);
????progressDialog.setTitle("我是一個進度條Dialog");
????progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
????progressDialog.setMax(MAX_PROGRESS);
????progressDialog.show();
????/* 模擬進度增加的過程
?????* 新開一個線程,每個100ms,進度增加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();
????????????????}
????????????}
????????????// 進度達到最大值后,窗口消失
????????????progressDialog.cancel();
????????}
????}).start();
}

2.7 編輯Dialog(圖8)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void showInputDialog() {
????/*@setView 裝入一個EditView
?????*/
????final EditText editText = new EditText(MainActivity.this);
????AlertDialog.Builder inputDialog =
????????new AlertDialog.Builder(MainActivity.this);
????inputDialog.setTitle("我是一個輸入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)

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void showCustomizeDialog() {
????/* @setView 裝入自定義View ==> R.layout.dialog_customize
?????* 由于dialog_customize.xml只放置了一個EditView,因此和圖8一樣
?????* dialog_customize.xml可自定義更復雜的View
?????*/
????AlertDialog.Builder customizeDialog =
????????new AlertDialog.Builder(MainActivity.this);
????final View dialogView = LayoutInflater.from(MainActivity.this)
????????.inflate(R.layout.dialog_customize,null);
????customizeDialog.setTitle("我是一個自定義Dialog");
????customizeDialog.setView(dialogView);
????customizeDialog.setPositiveButton("確定",
????????new DialogInterface.OnClickListener() {
????????@Override
????????public void onClick(DialogInterface dialog, int which) {
????????????// 獲取EditView中的輸入內容
????????????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.復寫回調函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 復寫Builder的create和show函數,可以在Dialog顯示前實現必要設置
?* 例如初始化列表、默認選項等
?* @create 第一次創建時調用
?* @show 每次顯示時調用
?*/
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("我是一個列表Dialog");
????listDialog.setItems(items, new DialogInterface.OnClickListener() {
????????@Override
????????public void onClick(DialogInterface dialog, int which) {
????????????// ...To-do
????????}
????});
????/* @setOnDismissListener Dialog銷毀時調用
?????* @setOnCancelListener Dialog關閉時調用
?????*/
????listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
????????public void onDismiss(DialogInterface dialog) {
????????????Toast.makeText(getApplicationContext(),
????????????????"Dialog被銷毀了",
????????????????Toast.LENGTH_SHORT).show();
????????}
????});
????listDialog.show();
}

轉載于:https://www.cnblogs.com/z2qfei/p/7833226.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/251521.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/251521.shtml
英文地址,請注明出處:http://en.pswp.cn/news/251521.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java 多線程 之 suspend掛起 線程實例

http://www.verejava.com/?id16992945731073 package com.suspend.resume; /*題目: 人們在火車站的售票窗口排隊買火車票1. 北京西站開門2. 打開售票窗口3. 北京西站有10張去長沙的票4. 打開2個售票窗口, 5 假設每個售票窗口每隔1秒鐘買完一張票1. 根據 名詞 找類人們(Person…

算法 --- 插入排序的JS實現

let A [5, 2, 4, 6, 1 ,3];// 插入排序 insertionSort (A) > {console.log("原數組>>>", A);for (let j1; j<A.length; j) {let key A[j];i j -1;while ( i > -1 && A[i] > key) {A[i1] A[i];i i-1;}A[i 1] key;}console.log(&q…

SAFESHE錯誤

今天寫驅動編譯的時候遇到一個問題&#xff0c;link一個比較老的lib時&#xff0c;報錯&#xff1a; error LNK2026: module unsafe for SAFESEH image 解決辦法&#xff1a; 在Source文件中加入一行 NO_SAFESEHTRUE 編譯時候執行 build -cZg轉載于:https://www.cnblogs.com/fa…

python之正則(一)

1.常用正則表達式: \d:數字[0-9] &#xff0c;實例:a\dc -> a1c\D:非數字[^\d],實例:a\Dc -> abc\s:空白字符[<空格>\t\r\n\f\v] 實例:a\sc ->a c\S:非空白字符[^\s] 實例:a\Sc ->abc\w:單詞字符[A-Za-z0-9_] 實例:a\wc ->abc\W:非單詞字符[^\W] *:匹配前…

邏輯讀、物理讀

邏輯讀、物理讀、預讀的基本概念轉載于:https://www.cnblogs.com/mySerilBlog/p/9208215.html

算法 --- 歸并排序的js實現

let mergeSort (A, p, q, r) > {console.log("原數組>>>", A);let n1 q - p 1;let n2 r - q;let L new Array();let R new Array();for (let i 1; i < n1 1; i) {L[i -1] A[p i - 1];}for (let j 1; j < n2 1; j) {R[j-1] A[q j];}L[…

c#中的代理和事件

事件&#xff08;event&#xff09;是一個非常重要的概念&#xff0c;我們的程序時刻都在觸發和接收著各種事件&#xff1a;鼠標點擊事件&#xff0c;鍵盤事件&#xff0c;以及處理操作系統的各種事件。所謂事件就是由某個對象發出的消息。比如用戶按下了某個按鈕&#xff0c;某…

個人技術博客

一. Volley框架 在進行和服務器交互的時候需要發送請求&#xff0c;發現了volley這個好用易上手的框架。volley是一個異步網絡通信框架&#xff0c;它的優點在于輕量級、適用于量小但傳送頻繁的請求操作 搭建請求的第一步就是新建一個請求隊列RequestQueue queue Volley.newRe…

軟件構造 第一章第二節 軟件開發的質量屬性

?軟件構造 第一章第二節 軟件開發的質量屬性 1.軟件系統質量指標 External quality factors affect users 外部質量因素影響用戶 Internal quality factors affect the software itself and its developers 內部質量因素影響軟件本身和它的開發者 External quality results fr…

css --- 讓不同的瀏覽器加載不同的CSS

// 通過條件注釋讓不同的瀏覽器加載不同的CSS <!--[if !IE]><!--> 除IE外都可識別 <!--<![endif]--> <!--[if IE]><!--> 所有的IE可識別 <![endif]--> <!--[if IE 6]> 僅IE6可識別 <![endif]--> <!--[if lt IE 6]> I…

??? ?? ??.??

abcdefg a?? abca abcbca abcabcdeda Cc ?? ??? [a] [ac] [a-c] [Cc] ??? 1>* ( 0~???) 2> (1~???) 3.? () 4 {1,2} {Min,Max} [??]*{} ???.??…

css自媒體查詢

準備工作1&#xff1a;設置Meta標簽 首先我們在使用Media的時候需要先設置下面這段代碼&#xff0c;來兼容移動設備的展示效果&#xff1a; <meta name"viewport" content"widthdevice-width, initial-scale1.0, maximum-scale1.0, user-scalableno">…

css --- 清除浮動

有時我們需要用到浮動,但又不想由于浮動的某些特性影響布局,這時就需要清除浮動 清除浮動主要應用的是CSS中的clear屬性,clear屬性定義了元素的哪一側不允許出現浮動元素. 下面是兩種應用比較廣泛的清除浮動的方法: // 在需要的地方添加定義了clear:both的空標簽 <style>…

數據可視化實現技術(canvas/svg/webGL)

數據可視化的實現技術和工具比較轉載于:https://www.cnblogs.com/knuzy/p/9215632.html

Python 字符串操作(string替換、刪除、截取、復制、連接、比較、查找、包含、大小寫轉換、分割等)...

http://www.cnblogs.com/huangcong/archis.strip() .lstrip() .rstrip(,) 去空格及特殊符號復制字符串Python1 #strcpy(sStr1,sStr2)2 sStr1 strcpy3 sStr2 sStr14 sStr1 strcpy25 print sStr2連接字符串Python1 #strcat(sStr1,sStr2)2 sStr1 strcat3 sStr2 append4 sStr1…

java 將一個非空文件夾拷貝到另一個地方

沒有處理異常&#xff0c;只是簡單的拋出了。需要捕獲的需修改一下。 public class Test001 { //把一個文件夾或文件移到另一個地方去。 public static void main(String[] args) throws Exception { File filenew File("D:\\testFolder"); new Test001().copyFileTo…

Python環境 及安裝

windows 1、下載安裝包 https://www.python.org/downloads/2、安裝默認安裝路徑&#xff1a;C:\python273、配置環境變量【右鍵計算機】--》【屬性】--》【高級系統設置】--》【高級】--》【環境變量】--》【在第二個內容框中找到 變量名為Path 的一行&#xff0c;雙擊】 -->…

MUI主界面菜單同時移動主體部分不出滾動條解決

mOffcanvas(側滑導航-主界面、菜單同時移動) 生成代碼 增加列表滾動OK 增加幻燈片就掛了 百度了半天 沒發現問題 后來想起官網的一句話 除頂部導航、底部選項卡兩個控件之外&#xff0c;其它控件都建議放在.mui-content控件內&#xff0c;在Hbuilder中輸入mbody&#xff0c;可快…

范圍查詢 BETWEEN AND

查詢&#xff1a;從表t_student里 查找 id 在1~10 之間的學生信息&#xff0c;并顯示 id,name,age,email 信息 SELECT id,name,age,email FROM t_student WHERE id BETWEEN 1 AND 10轉載于:https://www.cnblogs.com/hello-dummy/p/9216720.html

css --- 應用媒介查詢制作響應式導航欄

以上導航會自動適應各個尺寸的屏幕 代碼如下: <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <meta name"viewport" content"widthdevice-width, initial-scale1.0"> <meta name"apple-mobile-w…