場景 app中常見的對話框。 簡單的帶確定取消按鈕的對話框 帶列表的對話框 帶單項選擇的對話框 帶多項選擇的對話框 註: 博客: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 將佈局改為L ...
場景
app中常見的對話框。
簡單的帶確定取消按鈕的對話框
帶列表的對話框
帶單項選擇的對話框
帶多項選擇的對話框
註:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
將佈局改為LinearLayout,並通過android:orientation="vertical">設置為垂直佈局。並添加四個按鈕
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".AlertDialogActivity"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#090808" android:background="#F44336" android:text="1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#2196F3" android:background="#FF9800" android:text="2" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#F44336" android:background="#43b243" android:text="3" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#9C27B0" android:background="#f9671e" android:text="4" /> </LinearLayout>
然後來到Activity
帶確定取消按鈕的對話框
// 獲取“顯示帶取消、確定按鈕的對話框”按鈕 Button button1 = (Button) findViewById(R.id.button1); // 為“顯示帶取消、確定按鈕的對話框”按鈕添加單擊事件監聽器 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建對話框對象 這裡要通過Builder來創建 AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create(); alertDialog.setIcon(R.drawable.dog); //設置對話框的圖標 alertDialog.setTitle("公眾號:"); //設置對話框的標題 alertDialog.setMessage("霸道的程式猿"); //設置要顯示的內容 //sdk版本問題 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { //添加取消按鈕 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您單擊了否按鈕", Toast.LENGTH_SHORT).show(); } }); } //添加確定按鈕 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您單擊了是按鈕 ", Toast.LENGTH_SHORT).show(); } }); } alertDialog.show(); //顯示對話框 } });
帶列表的對話框
// 獲取“顯示帶列表的對話框”按鈕 Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建水果字元串數組 final String[] items = new String[]{"蘋果", "橘子", "香蕉", "西瓜"}; //創建列表對話框對象 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); //設置對話框的圖標 builder.setIcon(R.drawable.dog); //設置對話框的標題 builder.setTitle("請選擇你喜歡的水果:"); //添加列表項 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您選擇了" + items[which], Toast.LENGTH_SHORT).show(); } }); builder.create().show(); // 創建對話框並顯示 } });
帶單項選擇的對話框
// 獲取“顯示帶單選列表項的對話框”按鈕 Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建動物字元串數組 final String[] items = new String[]{"小貓", "小狗", "烏龜", "金魚", "小豬"}; // 顯示帶單選列表項的對話框 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(R.drawable.dog); //設置對話框的圖標 builder.setTitle("如果讓你選擇,你最喜歡哪一個:"); //設置對話框的標題 builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您選擇了" + items[which], Toast.LENGTH_SHORT).show(); //顯示選擇結果 } }); //添加確定按鈕 builder.setPositiveButton("確定", null); builder.create().show(); // 創建對話框並顯示 } });
帶多項選擇的對話框
首先聲明兩個變數用來存儲各列表項要顯示的內容和記錄各列表項的狀態
private boolean[] checkedItems;//記錄各列表項的狀態 private String[] items;//各列表項要顯示的內容
然後
// 獲取“顯示帶多選列表項的對話框”按鈕 Button button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkedItems = new boolean[]{false, true, false, true, false}; //記錄各列表項的狀態 //各列表項要顯示的內容 items = new String[]{"開心消消樂", "球球大作戰", "歡樂鬥地主", "夢幻西游", "超級瑪麗"}; // 顯示帶單選列表項的對話框 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(R.drawable.dog); //設置對話框的圖標 builder.setTitle("請選擇您喜愛的游戲:"); //設置對話框標題 builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; //改變被操作列表項的狀態 } }); //為對話框添加“確定”按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for (int i = 0; i < checkedItems.length; i++) { //當選項被選擇時 if (checkedItems[i]) { //將選項的內容添加到result中 result += items[i] + "、"; } } //當result不為空時,通過消息提示框顯示選擇的結果 if (!"".equals(result)) { //去掉最後面添加的“、”號 result = result.substring(0, result.length() - 1); Toast.makeText(AlertDialogActivity.this, "您選擇了[ " + result + " ]", Toast.LENGTH_LONG).show(); } } }); builder.create().show(); // 創建對話框並顯示 } });
完整示例代碼
package com.badao.relativelayouttest; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class AlertDialogActivity extends AppCompatActivity { private boolean[] checkedItems;//記錄各列表項的狀態 private String[] items;//各列表項要顯示的內容 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert_dialog); // 獲取“顯示帶取消、確定按鈕的對話框”按鈕 Button button1 = (Button) findViewById(R.id.button1); // 為“顯示帶取消、確定按鈕的對話框”按鈕添加單擊事件監聽器 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建對話框對象 這裡要通過Builder來創建 AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create(); alertDialog.setIcon(R.drawable.dog); //設置對話框的圖標 alertDialog.setTitle("公眾號:"); //設置對話框的標題 alertDialog.setMessage("霸道的程式猿"); //設置要顯示的內容 //sdk版本問題 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { //添加取消按鈕 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您單擊了否按鈕", Toast.LENGTH_SHORT).show(); } }); } //添加確定按鈕 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) { alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您單擊了是按鈕 ", Toast.LENGTH_SHORT).show(); } }); } alertDialog.show(); //顯示對話框 } }); // 獲取“顯示帶列表的對話框”按鈕 Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建水果字元串數組 final String[] items = new String[]{"蘋果", "橘子", "香蕉", "西瓜"}; //創建列表對話框對象 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); //設置對話框的圖標 builder.setIcon(R.drawable.dog); //設置對話框的標題 builder.setTitle("請選擇你喜歡的水果:"); //添加列表項 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您選擇了" + items[which], Toast.LENGTH_SHORT).show(); } }); builder.create().show(); // 創建對話框並顯示 } }); // 獲取“顯示帶單選列表項的對話框”按鈕 Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創建動物字元串數組 final String[] items = new String[]{"小貓", "小狗", "烏龜", "金魚", "小豬"}; // 顯示帶單選列表項的對話框 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(R.drawable.dog); //設置對話框的圖標 builder.setTitle("如果讓你選擇,你最喜歡哪一個:"); //設置對話框的標題 builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "您選擇了" + items[which], Toast.LENGTH_SHORT).show(); //顯示選擇結果 } }); //添加確定按鈕 builder.setPositiveButton("確定", null); builder.create().show(); // 創建對話框並顯示 } }); // 獲取“顯示帶多選列表項的對話框”按鈕 Button button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkedItems = new boolean[]{false, true, false, true, false}; //記錄各列表項的狀態 //各列表項要顯示的內容 items = new String[]{"開心消消樂", "球球大作戰", "歡樂鬥地主", "夢幻西游", "超級瑪麗"}; // 顯示帶單選列表項的對話框 AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(R.drawable.dog); //設置對話框的圖標 builder.setTitle("請選擇您喜愛的游戲:"); //設置對話框標題 builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; //改變被操作列表項的狀態 } }); //為對話框添加“確定”按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for (int i = 0; i < checkedItems.length; i++) { //當選項被選擇時 if (checkedItems[i]) { //將選項的內容添加到result中 result += items[i] + "、"; } } //當result不為空時,通過消息提示框顯示選擇的結果 if (!"".equals(result)) { //去掉最後面添加的“、”號 result = result.substring(0, result.length() - 1); Toast.makeText(AlertDialogActivity.this, "您選擇了[ " + result + " ]", Toast.LENGTH_LONG).show(); } } }); builder.create().show(); // 創建對話框並顯示 } }); } }