Android中創(chuàng)建對話框(確定取消對話框、單選對話框、多選對話框)實(shí)例代碼
Android中可以創(chuàng)建三種對話框、確定取消對話框、單選對話框、多選對話框
android中的確定取消對話框演示示例
Android中使用單選對話框的演示案例
android中使用多選對話框的演示案例
實(shí)現(xiàn)代碼如下
修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.fyt.dialogdemo.MainActivity" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定取消對話框" android:onClick="Clicked1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="單選對話框" android:onClick="Clicked2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多選對話框" android:onClick="Clicked3"/> </LinearLayout>
最后修改MainActivity.Java中的代碼
package com.fyt.dialogdemo; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //確定取消對話框按鈕響應(yīng)函數(shù) public void Clicked1(View view) { //使用創(chuàng)建器創(chuàng)建單選對話框 AlertDialog.Builder builder = new Builder(this); //設(shè)置對話框的圖標(biāo) builder.setIcon(android.R.drawable.alert_light_frame); //設(shè)置對話框的標(biāo)題 builder.setTitle("欲練此功必先自宮"); //設(shè)置文本 builder.setMessage("林平之,想清楚哦"); //設(shè)置確定按鈕 builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "感謝使用本軟件,再見", Toast.LENGTH_SHORT).show(); } }); //設(shè)置取消按鈕 builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "若不自宮,一定不成功", Toast.LENGTH_SHORT).show(); } }); //使用創(chuàng)建器生成一個(gè)對話框?qū)ο? AlertDialog ad = builder.create(); ad.show(); } //單選對話框按鈕響應(yīng)函數(shù) public void Clicked2(View view) { AlertDialog.Builder builder = new Builder(this); //設(shè)置單選對話框的標(biāo)題 builder.setTitle("請選擇性別"); //創(chuàng)建字符串?dāng)?shù)組,用來設(shè)置單選對話框中的條目 final String[] items = {"男", "女"}; //創(chuàng)建單選對話框 //第一個(gè)參數(shù):單選對話框中顯示的條目所在的字符串?dāng)?shù)組 //第二個(gè)參數(shù):默認(rèn)選擇的條目的下標(biāo)(-1表示默認(rèn)沒有選擇任何條目) //第三個(gè)參數(shù):設(shè)置事件監(jiān)聽 builder.setSingleChoiceItems(items, -1, new OnClickListener() { //which:用戶所選的條目的下標(biāo) //dialog:觸發(fā)這個(gè)方法的對話框 @Override public void onClick(DialogInterface dialog, int which) { //使用吐司對話框提示用戶選擇的性別 Toast.makeText( MainActivity.this, "您選擇的性別是:" + items[which], Toast.LENGTH_SHORT).show(); //關(guān)閉對話框 dialog.dismiss(); } }); //顯示單選對話框 builder.show(); } //多選對話框按鈕響應(yīng)函數(shù) public void Clicked3(View view) { AlertDialog.Builder builder = new Builder(this); //設(shè)置多選對話框的標(biāo)題 builder.setTitle("請選擇您覺得帥的人"); //創(chuàng)建字符串?dāng)?shù)組,用于設(shè)置多選對話框中的選項(xiàng) final String[] items = { "侃哥", "趙帥哥", "趙老師", "趙師兄" }; //創(chuàng)建boolean數(shù)組,用于設(shè)置多選對話框中的條目是否被選中了 final boolean[] checkedItems = { false, false, false, false }; //創(chuàng)建多選對話框 //第一個(gè)參數(shù):多選對話框中條目所在的字符串?dāng)?shù)組 //第二個(gè)參數(shù):條目是否被選中的標(biāo)記所在的數(shù)組 //第三個(gè)參數(shù):創(chuàng)建事件監(jiān)聽 builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { //which:用戶點(diǎn)擊的條目的下標(biāo) //isChecked:用戶是選中該條目還是取消該條目 @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }); //設(shè)置一個(gè)確定按鈕 builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String text = ""; //記錄多選對話框中用戶選中的條目 for(int i = 0; i < 4; i++) { text += checkedItems[i]? items[i] + "," : ""; } //使用吐司對話框顯示用戶選中的條目 Toast.makeText(MainActivity.this, text, 0).show(); //隱藏多選對話框 dialog.dismiss(); } }); //顯示多選對話框 builder.show(); } }
- Android實(shí)現(xiàn)彈出列表、單選、多選框
- Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
- Android ListView實(shí)現(xiàn)單選及多選等功能示例
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對話框功能
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
- Android實(shí)現(xiàn)單選與多選對話框的代碼
- Android ListView構(gòu)建支持單選和多選的投票項(xiàng)目
- Android單選多選按鈕的使用方法
相關(guān)文章
Android app啟動時(shí)黑屏或者白屏的原因及解決辦法
這篇文章主要介紹了Android app啟動時(shí)黑屏或者白屏的原因及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-09-09Android BottomNavigationView底部導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了Android BottomNavigationView底部導(dǎo)航效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)氣泡布局/彈窗效果,可控制氣泡尖角方向及偏移量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android?TextView的maxEms和maxLength屬性區(qū)別
這篇文章主要為大家介紹了Android?TextView的maxEms和maxLength屬性區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Flutter實(shí)現(xiàn)不同縮放動畫效果詳解
這篇文章主要為大家詳細(xì)介紹了Flutter利用不同組件(ScaleTransition、SizeTransition、AnimatedSize和AnimatedBuilder)實(shí)現(xiàn)不同縮放動畫效果,感興趣的可以動手嘗試一下2022-06-06詳解Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密
對于已經(jīng)ROOT的手機(jī)來說的沒有任何安全性可以,一旦被利用將會導(dǎo)致數(shù)據(jù)庫數(shù)據(jù)的泄漏,本篇文章主要介紹了Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12Android listview與adapter詳解及實(shí)例代碼
本文主要介紹Android listview與adapter的知識詳解,這里整理了相關(guān)資料及實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-09-09