•AlertDialog簡介
AlertDialog 可以在當前界面彈出一個對話框;
這個對話框是置頂於所有界面元素之上的,能夠屏蔽掉其他控件的交互能力;
因此, AlertDialog 一般用於提示一些非常重要的內容或者警告信息;
AlertDialog 並不能直接 new 出來;
如果我們要創建 AlertDialog 的話,我們需要使用到該類中的一個靜態內部類 public static class Builder ;
然后來調用 AlertDialog 里的相關方法,來對 AlertDialog 進行定制;
最后調用 show() 方法來顯示我們的 AlertDialog 對話框;
•內置方法
setTitle : 為對話框設置標題
setIcon : 為對話框設置圖標
setMessage : 為對話框設置內容
setPositiveButton : 為對話框設置確定按鈕的點擊事件
setNegativeButton() : 為對話框設置取消按鈕的點擊事件
setNeutralButton() : 為對話框設置中立按鈕的點擊事件
setItems() : 創建簡單的列表對話框
setSingleChoiceItems() : 創建單選列表對話框
setMultiChoiceItems() : 創建多選列表對話框
•setCancelable()&setCanceledOnTouchOutside()
setCancelable(false)
- dialog 彈出后,點擊屏幕或返回鍵,dialog 不消失
setCanceledOnTouchOutside(false)
- dialog彈出后,點擊屏幕,dialog不消失
- 點擊返回鍵 dialog 才會消失
•實戰演練之點餐--創建多選列表對話框
效果展示圖
准給工作
新建一個項目,選擇 Empty Activity,這樣,系統為我們自動生成了 MainActivity.java 喝 activity_main.xml 文件;
在 activity_main.xml 中添加如下布局文件;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <Button android:id="@+id/btn_order" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ORDER"/> </LinearLayout>在該布局中只放置了一個 Button 按鈕;
修改 MainActivity.java 中的代碼;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { final private Context mContext = MainActivity.this; private Button Btn; private AlertDialog alert = null; private AlertDialog.Builder builder = null; private boolean[] checkItems; private String[] menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Btn = findViewById(R.id.btn_order); Btn.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.btn_order: final String[] menu = new String[]{"豬肉燉粉條", "小雞燉蘑菇", "宮保雞丁", "可樂雞翅"}; checkItems = new boolean[]{false, false, false, false}; alert = null; builder = new AlertDialog.Builder(mContext); alert = builder.setTitle("菜單") .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkItems[which] = isChecked; } }) .setPositiveButton("提交", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int cnt = 0; String result = "客官,您點了“"; boolean flag = false; for (int i = 0; i < checkItems.length; ++i) { if (checkItems[i]) { cnt++; if (flag == true) result += ","; result += menu[i]; flag = true; } } result += "”,共" + cnt + "道菜!"; Toast.makeText(mContext, result, Toast.LENGTH_LONG).show(); } }) .create();//通過 create() 生成一個AlertDialog 對象 alert.show(); break; default: break; } } }實現過程中出現的 bug
出錯原因
setMultiChoiceItems() 的第二個參數應該是 boolean[] 類型,而我的 checkItems 是 Boolean[] 類型;
解決方案
將 checkItems 改成 boolean[] 類型就行了;
參考資料:Android 創建多選菜單,寫setMultiChoiceItems()出現問題,提示不存在該方法的解決方法
注意:如果你是通過 AlertDialog 調用 .show() 方法的話,記得在 builder.setXXXX 的最后添加 .create() 生成一個AlertDialog對象;
•聲明
參考資料
【菜鳥教程】
【第一行代碼(第二版)】


