日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 8种对话框(Dialog)使用方法汇总

發布時間:2023/12/9 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(); }

總結

以上是生活随笔為你收集整理的android 8种对话框(Dialog)使用方法汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。