Android中使用AlertDialog實現幾種不一樣的對話框

場景

app中常見的對話框。android

簡單的帶肯定取消按鈕的對話框編程

 

 

帶列表的對話框數組

 

 

帶單項選擇的對話框app

 

 

帶多項選擇的對話框ide

 

 

 

注:佈局

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。ui

實現

將佈局改成LinearLayout,並經過android:orientation="vertical">設置爲垂直佈局。並添加四個按鈕this

<?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>

 

而後來到Activityspa

帶肯定取消按鈕的對話框

 

       // 獲取「顯示帶取消、肯定按鈕的對話框」按鈕
        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();                                    // 建立對話框並顯示
            }
        });

 

帶多項選擇的對話框

首先聲明兩個變量用來存儲各列表項要顯示的內容和記錄各列表項的狀態.net

    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();                                // 建立對話框並顯示
            }
        });
    }
}

 
相關文章
相關標籤/搜索