最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

提示信息控件AlertDialog對話框詳解

 更新時間:2023年04月13日 15:12:48   作者:向陽逐夢  
這篇文章主要為大家介紹了提示信息控件AlertDialog對話框的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

提要

本節(jié)繼續(xù)給大家?guī)硎秋@示提示信息的第三個控件AlertDialog(對話框),同時它也是其他Dialog的的父類!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父類是:Dialog!另外,不像前面學(xué)習(xí)的Toast和Notification,AlertDialog并不能直接new出來,如果你打開AlertDialog的源碼,會發(fā)現(xiàn)構(gòu)造方法是protected的,如果我們要創(chuàng)建AlertDialog的話,我們需要使用到該類中的一個靜態(tài)內(nèi)部類:public static class Builder,然后來調(diào)用AlertDialog里的相關(guān)方法,來對AlertDialog進行定制,最后調(diào)用show()方法來顯示我們的AlertDialog對話框!

1.基本使用流程

  • Step 1:創(chuàng)建AlertDialog.Builder對象;
  • Step 2:調(diào)用setIcon() 設(shè)置圖標(biāo),setTitle() 或setCustomTitle() 設(shè)置標(biāo)題;
  • Step 3:設(shè)置對話框的內(nèi)容:setMessage() 還有其他方法來指定顯示的內(nèi)容;
  • Step 4:調(diào)用setPositive/Negative/NeutralButton() 設(shè)置:確定,取消,中立按鈕;
  • Step 5:調(diào)用create() 方法創(chuàng)建這個對象,再調(diào)用show() 方法將對話框顯示出來;

2.幾種常用的對話框使用示例

運行效果圖:

核心代碼:

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_dialog_one;
    private Button btn_dialog_two;
    private Button btn_dialog_three;
    private Button btn_dialog_four;
    private Context mContext;
    private boolean[] checkItems;
    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        bindView();
    }
    private void bindView() {
        btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one);
        btn_dialog_two = (Button) findViewById(R.id.btn_dialog_two);
        btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three);
        btn_dialog_four = (Button) findViewById(R.id.btn_dialog_four);
        btn_dialog_one.setOnClickListener(this);
        btn_dialog_two.setOnClickListener(this);
        btn_dialog_three.setOnClickListener(this);
        btn_dialog_four.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //普通對話框
            case R.id.btn_dialog_one:
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("系統(tǒng)提示:")
                        .setMessage("這是一個最普通的AlertDialog,\n帶有三個按鈕,分別是取消,中立和確定")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你點擊了取消按鈕~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你點擊了確定按鈕~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你點擊了中立按鈕~", Toast.LENGTH_SHORT).show();
                            }
                        }).create();             //創(chuàng)建AlertDialog對象
                alert.show();                    //顯示對話框
                break;
            //普通列表對話框
            case R.id.btn_dialog_two:
                final String[] lesson = new String[]{"語文", "數(shù)學(xué)", "英語", "化學(xué)", "生物", "物理", "體育"};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("選擇你喜歡的課程")
                        .setItems(lesson, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你選擇了" + lesson[which], Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alert.show();
                break;
            //單選列表對話框
            case R.id.btn_dialog_three:
                final String[] fruits = new String[]{"蘋果", "雪梨", "香蕉", "葡萄", "西瓜"};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("選擇你喜歡的水果,只能選一個哦~")
                        .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你選擇了" + fruits[which], Toast.LENGTH_SHORT).show();
                            }
                        }).create();
                alert.show();
                break;
            //多選列表對話框
            case R.id.btn_dialog_four:
                final String[] menu = new String[]{"水煮豆腐", "蘿卜牛腩", "醬油雞", "胡椒豬肚雞"};
                //定義一個用來記錄個列表項狀態(tài)的boolean數(shù)組
                checkItems = new boolean[]{false, false, false, false};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.ic_icon_fish)
                        .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) {
                                String result = "";
                                for (int i = 0; i < checkItems.length; i++) {
                                    if (checkItems[i])
                                        result += menu[i] + " ";
                                }
                                Toast.makeText(getApplicationContext(), "客官你點了:" + result, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alert.show();
                break;
        }
    }
}

布局就是四個簡單的按鈕,這里就不貼出來了,用法非常簡單~無非就是創(chuàng)建一個Builder對象后,進行相關(guān)設(shè)置,然后create()生成一個AlertDialog對象,最后調(diào)用show()方法將AlertDialog顯示出來而已!另外,細心的你可能發(fā)現(xiàn)我們點擊對話框的外部區(qū)域,對話框就會消失,我們可以為builder設(shè)置setCancelable(false) 即可解決這個問題!

3.通過Builder的setView()定制顯示的AlertDialog

我們可以自定義一個與系統(tǒng)對話框不同的布局,然后調(diào)用setView()將我們的布局加載到AlertDialog上,上面我們來實現(xiàn)這個效果:

運行效果圖:

關(guān)鍵代碼:

首先是兩種不同按鈕的selctor的drawable文件:

btn_selctor_exit.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/iv_icon_exit_pressed"/>
    <item android:drawable="@mipmap/iv_icon_exit_normal"/>
</selector>

btn_selctor_choose.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/bg_btn_pressed"/>
    <item android:drawable="@mipmap/bg_btn_normal"/>
</selector>

接著是自定義的Dialog布局:view_dialog_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/titlelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#53CC66"
        android:padding="5dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="提示信息"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_cancle"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/btn_selctor_exit" />
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/ly_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/titlelayout"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="通過setView()方法定制AlertDialog"
            android:textColor="#04AEDA"
            android:textSize="18sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="作者:Coder-pig"
            android:textColor="#04AEDA"
            android:textSize="18sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ly_detail"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_blog"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/btn_selctor_choose"
            android:text="訪問博客"
            android:textColor="#ffffff"
            android:textSize="20sp" />
        <Button
            android:id="@+id/btn_close"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/btn_selctor_choose"
            android:text="關(guān)閉"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </LinearLayout>
</RelativeLayout>  

最后是MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private Button btn_show;
    private View view_custom;
    private Context mContext;
    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        btn_show = (Button) findViewById(R.id.btn_show);
        //初始化Builder
        builder = new AlertDialog.Builder(mContext);
        //加載自定義的那個View,同時設(shè)置下
        final LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        view_custom = inflater.inflate(R.layout.view_dialog_custom, null,false);
        builder.setView(view_custom);
        builder.setCancelable(false);
        alert = builder.create();
        view_custom.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        view_custom.findViewById(R.id.btn_blog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "訪問博客", Toast.LENGTH_SHORT).show();
                Uri uri = Uri.parse("http://blog.csdn.net/coder_pig");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                alert.dismiss();
            }
        });
        view_custom.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "對話框已關(guān)閉~", Toast.LENGTH_SHORT).show();
                alert.dismiss();
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.show();
            }
        });
    }
}

以上就是提示信息控件AlertDialog對話框詳解的詳細內(nèi)容,更多關(guān)于AlertDialog 對話框的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Flutter學(xué)習(xí)之SliverList和SliverGird的使用詳解

    Flutter學(xué)習(xí)之SliverList和SliverGird的使用詳解

    Sliver的組件一般都用在CustomScrollView中,除了SliverAppBar之外,我們還可以為CustomScrollView添加List或者Grid來實現(xiàn)更加復(fù)雜的組合效果。本文就來聊聊SliverList和SliverGird的使用吧
    2023-02-02
  • Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)

    Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)

    這篇文章主要介紹了Android 自定義日期段選擇控件功能,開始時間-結(jié)束時間。本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android實現(xiàn)騰訊新聞的新聞類別導(dǎo)航效果

    Android實現(xiàn)騰訊新聞的新聞類別導(dǎo)航效果

    這篇文章主要介紹了Android實現(xiàn)騰訊新聞的新聞類別導(dǎo)航效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • Android實現(xiàn)簡易瀏覽器遇到問題的解決方法

    Android實現(xiàn)簡易瀏覽器遇到問題的解決方法

    這篇文章主要為大家詳細介紹了Android實現(xiàn)簡易瀏覽器遇到的一系列問題的解決方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android編程單選項框RadioGroup綜合應(yīng)用示例

    Android編程單選項框RadioGroup綜合應(yīng)用示例

    這篇文章主要介紹了Android編程單選項框RadioGroup用法,結(jié)合實例形式分析了Android單選按鈕組RadioGroup的定義與具體使用技巧,需要的朋友可以參考下
    2016-10-10
  • Android編程獲取控件寬和高的方法總結(jié)分析

    Android編程獲取控件寬和高的方法總結(jié)分析

    這篇文章主要介紹了Android編程獲取控件寬和高的方法,結(jié)合實例形式對比總結(jié)并分析了Android控件屬性的相關(guān)操作技巧,需要的朋友可以參考下
    2016-01-01
  • 詳解Android 多級聯(lián)動控件實現(xiàn)思路討論

    詳解Android 多級聯(lián)動控件實現(xiàn)思路討論

    這篇文章主要介紹了詳解Android 多級聯(lián)動控件實現(xiàn)思路討論,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android自定義Gradle插件的詳細過程

    Android自定義Gradle插件的詳細過程

    Groovy語言是一種jvm語言,最終也是編譯成class文件然后在jvm上執(zhí)行,所以所有的Java語言的特性Groovy都支持,我們可以完全混寫Java和Groovy,對Android自定義Gradle插件相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Android中JSON的4種解析方式使用和對比

    Android中JSON的4種解析方式使用和對比

    本文主要介紹了Android中JSON的4種解析方式使用和對比,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • Android中實現(xiàn)詞組高亮TextView方法示例

    Android中實現(xiàn)詞組高亮TextView方法示例

    高亮顯示大家應(yīng)該都不陌生,在開發(fā)中經(jīng)常會遇到這個需求,所以下面這篇文章主要給大家介紹了關(guān)于Android中實現(xiàn)詞組高亮TextView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10

最新評論

莱州市| 滨海县| 彭水| 新平| 江口县| 九龙城区| 广东省| 新源县| 特克斯县| 古蔺县| 新密市| 石嘴山市| 合肥市| 富锦市| 陈巴尔虎旗| 双柏县| 开封市| 顺义区| 赫章县| 寿阳县| 宝丰县| 环江| 邵阳县| 霸州市| 贵溪市| 东兰县| 剑阁县| 广元市| 大关县| 宁陕县| 新昌县| 来安县| 达拉特旗| 土默特右旗| 光泽县| 宁都县| 福建省| 肇东市| 剑川县| 上饶县| 阿合奇县|