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

本文為作者原創,轉載請注明出處:http://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();
}

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

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

相關文章

使用layui的layer組件做彈出層

官方文檔地址: http://www.layui.com/doc/modules/layer.html 本例演示效果: 當點擊申請提現時,出現申請提現框,并根據用戶輸入進行一些判斷,給出友好提示,比如: 代碼實現: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8&q…

C#之表達式樹使用

目的遇到一個場景需要接收一個表的列來進行動態排序&#xff0c;比如我想根據CreateTime進行正序排序&#xff0c;加上我使用的ORM框架是EFCore&#xff0c;那么我一下子就想到應該使用OrderBy&#xff0c;然后接收一個要排序的列query.OrderBy("CreateTime")但是這樣…

實現一個基于相等性比較的 GroupBy

實現一個基于相等性比較的 GroupByIntro在我們的系統里有些數據可能會有問題&#xff0c;數據源頭不在我們這里&#xff0c;數據不好修復&#xff0c;在做 GroupBy 的時候就會很痛苦&#xff0c;默認的 group by 會依賴于 HashCode &#xff0c;而某些場景下 HashCode 可能并不…

win7系統下載 ghost win7 Sp1 64位純凈3月版

win7系統下載 ghost win7 Sp1 64位純凈3月版 軟件名稱: Ghost Win7 Sp1 64位純凈3月版軟件語言: 簡體中文軟件大小: 5.25大小: GB發布日期: 2017-03-21文件名稱: ZJY_Ghost_win 7_X64_CJ201703.GHOM D 5: EB16DCD608A5CCFE34B58…

CrossPHP框架的常用操作

1. 在視圖控制器中使用$this->res()方法來生成資源文件的絕對路徑$this->res(css/style.css);生成的連接為http://youdomain.com/static/css/style.css2. 生成指定app名稱的連接$this->appUrl()第一個參數為基礎url, 第二個參數為app名稱, 第三個參數為 控制器:方法 第…

WPF-07 Style之觸發器

觸發器能夠在改變屬性值的時候&#xff0c;根據值變化執行操作&#xff0c;在不需要創建一個新的控件的情況下&#xff0c;可以動態的改變控件的外觀&#xff0c;當條件滿足時&#xff0c;觸發器可以改變任何屬性的值&#xff0c;觸發器通常定義在Style中&#xff0c;在窗體的根…

jdk自帶常用命令行工具使用

轉自&#xff1a;http://blog.csdn.net/winwill2012/article/details/46364923jps命令使用jps命令類似于Linux下的ps命令&#xff0c;用于列出當前正在運行的所有Java進程。基本用法直接運行不加任何參數就能列出所有java進程的pid和類的短名稱。例如&#xff1a;常用參數-q參數…

crossphp框架中,在模板中加載其他模板

這里說我自己做的項目的應用場景 要求是用layui框架的layer組件,實現彈出層效果,用原聲PHP無疑很容易做到,但是如果應用到crossphp框架流程就會非常麻煩 這里簡單講一下大致的步驟: 1. 在一個模板文件中應用layui的layer組件實現彈出框 index.tpl.php2. 從我們自己定義的路徑上…

for(auto c:s)與for(auto c:s)

在c11標準下可以執行的特殊格式的for循環語句&#xff0c;區別在于引用類型可以改變原來的值 #include<iostream> using namespace std; int main() {string s("hello world");for(auto c:s)ct;cout<<s<<endl;//結果為hello worldfor(auto &c:…

MASA Framework的MinimalAPIs應用

在以前的MVC引用程序中&#xff0c;控制器是一個功能齊全的框架&#xff0c;但它偏重&#xff0c;因此在.Net6.0官方引入了MinimalAPIs&#xff0c;即最小API&#xff0c;與MVC相比&#xff0c;它足夠的簡潔&#xff0c;適合小型服務來使用&#xff0c;下面就讓我們看看如何使用…

【轉】Java開發必須要知道的知識體系

Java Java是一門超高人氣編程語言&#xff0c;擁有跨平臺、面向對象、泛型編程等特性。在TIOBE編程語言排行榜中&#xff0c;連續奪得第一寶座&#xff0c;而且國內各大知名互聯網公司&#xff0c;后端開發首選語言&#xff1a;非Java莫屬。今天只是梳理下Java知識體系&#xf…

CrossPHP--在我們用ajax,js取不到指定數據時,我們可以換一種方式

項目中遇到的問題: 需求: 用的是layui的laypage組件,進行分頁操作,熟悉layui的朋友都知道,laypage需要從服務端給其一個總條數, 但是在進行ajax請求時出了問題, 我是這樣定義的但是調用的時候卻無法將數值直接返回回去,所以這里只能更換一種思路 在控制器中進行數據的查詢,然后…

VS 代碼行數統計

按CTRLSHIFTF (Find in files)&#xff0c;勾上支持正則表達式&#xff0c;然后輸入搜索內容&#xff1a; ^:b*[^:b#/].*$#開頭和/開頭或者空行都不計入代碼量。如果需要只統計代碼文件的代轉載于:https://www.cnblogs.com/sunlyk/p/7484728.html

MySQL設置從庫只讀模式

常見現象 運維工作中會經常維護MySQL主從服務器&#xff0c;當然Slave我們只是用于讀操作。 一般權限開通也只授權只讀賬號&#xff0c;但是有時候維護工作可能不是一個人在做&#xff0c;你不能保證其他同事都按照這個標準操作。 有同事可能會授權Slave庫MySQL賬號為all或者se…

尋找kernel32.dll的地址

為了尋找kernel32.dll的地址&#xff0c;可以直接輸出&#xff0c;也可以通過TEB,PEB等查找。 尋找TEB: dt _TEB nt!_TEB 0x000 NtTib : _NT_TIB 0x01c EnvironmentPointer : Ptr32 Void 0x020 ClientId : _CLIENT_ID 0x028 ActiveRpcHandle : Ptr32 Void 0x02c ThreadLocalSto…

layui彈出層使用(layer.alert / layer.open / layer.prompt )

一 layer.alert 效果圖: 代碼: //取消提現 function back(id) {layer.alert(真的要取消嗎, {skin: layui-layer-molv //樣式類名 自定義樣式,closeBtn: 1 // 是否顯示關閉按鈕,anim: 1 //動畫類型,btn: [確定,取消] //按鈕,icon: 6 // icon,yes:function(){return $.aj…

SkiaSharp 自繪彈幕效果

SkiaSharp 自繪彈幕效果控件名&#xff1a;SkiaSharpBarrage作者&#xff1a; 驚鏵原文鏈接&#xff1a; https://github.com/yanjinhuagood/SkiaSharpBarrage框架使用.NET60&#xff1b;Visual Studio 2022;項目使用 MIT 開源許可協議&#xff1b;接著上一篇 WPF 彈幕上期有…

JavaScript中this指向

一.重點來了&#xff0c;this指向問題&#xff1a;1.this指向之普通函數。 2.this指向之對象 3.this指向之構造函數 4.this指向之&#xff08;call,apply&#xff09;動態更改this指向。 二.具體分析如下 1.普通函數 // 第23行的調用者為null,this指向也為null,// 所以這時js把…

【python】python中的定義類屬性和對像屬性

python中變量是沒有類型的可以綁定任意類型&#xff0c;但是在語法上不能聲明變量。 那我們怎麼來聲名一個變量呢&#xff1f; fNone 這樣我們給著個變量綁定了以各None類型&#xff0c;我們隨時可用重新綁定其它類型。這樣我們起到了預先聲名變量的效果。 類中如何去定義類的…

提交Form表單,submit之前做js判斷處理

效果:在點擊提交按鈕時,首先進行js判斷, 如果不符合條件,則alert出提示信息,并return false. 主要點就在于給form表單添加一個onsubmit事件. 在onsubmit事件中定義的函數里進行js驗證處理.代碼 : <!DOCTYPE html> <html lang"en"> <head><meta …