Android : AlertDialog對話框、單選、多選、適配器-簡單應用

示例圖:

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

MainActivity.java

package com.example.myalertdialog;import androidx.appcompat.app.AppCompatActivity;import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;// AlertDialog對話框 應用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {//組件private Button btnOne,btnTwo,btnThree,btnFour,btnFive;private TextView textView;private Context context;//對話框private AlertDialog.Builder builder =null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context =this;btnOne = findViewById(R.id.btn_one);btnTwo = findViewById(R.id.btn_Two);btnThree = findViewById(R.id.btn_three);btnFour =findViewById(R.id.btn_four);btnFive = findViewById(R.id.btn_five);textView = findViewById(R.id.btn_tv);//設置事件btnOne.setOnClickListener(this);btnTwo.setOnClickListener(this);btnThree.setOnClickListener(this);btnFour.setOnClickListener(this);btnFive.setOnClickListener(this);}@Overridepublic void onClick(View v) {if(v.getId() == R.id.btn_one){//普通對話框builder = new AlertDialog.Builder(context);//標題builder.setTitle("游戲提示:");//logobuilder.setIcon(R.mipmap.a);//內容builder.setMessage("您確認要開始游戲嗎?");//確定按鈕builder.setPositiveButton("開始", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context ,"開始游戲",Toast.LENGTH_SHORT).show();}});//取消按鈕  null 表示什么都不做builder.setNegativeButton("取消",null);//中立按鈕builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context,"中立您選擇了"+which,Toast.LENGTH_SHORT).show();}});//創建 顯示builder.create().show();}else if (R.id.btn_Two == v.getId()) {//單選對話框 1 只能選一次//數據源final String[] arr = new String[]{"校長","老師","學生","家長"};//單選對話框builder = new AlertDialog.Builder(context);//標題builder.setTitle("請選擇您的身份:");//圖標builder.setIcon(R.mipmap.a);//內容builder.setItems(arr, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context,"您選擇了"+arr[which],Toast.LENGTH_SHORT).show();textView.setText(arr[which]);}});//取消按鈕builder.setNegativeButton("取消",null);//創建 顯示builder.create().show();} else if (v.getId() == R.id.btn_three) {// 單選對話框2 可反復選擇builder = new AlertDialog.Builder(context);//數據源 2final String[] arr = new String[]{"java","php","android","c++"};//標題builder.setTitle("請選擇課程:");//圖標builder.setIcon(R.mipmap.a);//內容   默認選擇項 -1表示不選擇  0表示選中第一個builder.setSingleChoiceItems(arr, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context,"您選擇了"+arr[which],Toast.LENGTH_SHORT).show();textView.setText(arr[which]);}});//取消按鈕builder.setNegativeButton("取消",null);//創建 顯示builder.create().show();} else if (v.getId() == R.id.btn_four) {// 多選對話框builder = new AlertDialog.Builder(context);//選擇后的集合List<String> datas = new ArrayList<>();//數據源final String[] arr = {"胡蘿卜炒肉","辣椒炒蛋","水煮活魚","清蒸龍蝦"};//標題builder.setTitle("請選擇菜單:");//圖標builder.setIcon(R.mipmap.a);//內容 setMultiChoiceItems// 數據源,默認選中哪幾個, null 表示一個都不選// new boolean[]{false, false, true, true}builder.setMultiChoiceItems(arr, null, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {if(isChecked){datas.add(arr[which]);}else{datas.remove(arr[which]);}}});//確定按鈕 setPositiveButtonbuilder.setPositiveButton("確認", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context,"您選擇了"+datas.toString(),Toast.LENGTH_SHORT).show();textView.setText(datas.toString());}});//取消builder.setNeutralButton("取消",null);//創建 顯示builder.create().show();}if(v.getId() == R.id.btn_five){// 適配器對話框 調用寫的方法builder = AlertSimplAdapter.getAlertBuilder(context);//創建 顯示builder.create().show();}}}

寫一個方法類?AlertSimplAdapter.java

package com.example.myalertdialog;import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.SimpleAdapter;
import android.widget.Toast;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class AlertSimplAdapter {//圖片private static final int[] arrImg = new int[]{R.mipmap.a, R.mipmap.b, R.mipmap.c,R.mipmap.d, R.mipmap.e, R.mipmap.f,R.mipmap.g, R.mipmap.h, R.mipmap.i,R.mipmap.a,  R.mipmap.a, R.mipmap.a,R.mipmap.a,  R.mipmap.a, R.mipmap.a};//標題private static final String[] arrTitle = {"張三","李四","張三豐","小學群聊","周芷若","周先生","老領導","玖龍璽","阿道夫","張三","李四","張三豐","張三","李四","張三豐"};//內容private static String[] arrContent =new String[]{"早啊!","你在干啥","你最近在練什么武功","呼叫XXX家長","最近咋樣","吃飯了嗎?","啥時候有空喝茶呀?",".....","小伙子,學魔法嗎?","早啊!","你在干啥","你最近在練什么武功","早啊!","你在干啥","你最近在練什么武功"};//時間private static String[] arrTime = {"剛剛","昨天","11-20","10-1","2023-11-20","17:30","9:00","10-1","2000-01-01","剛剛","昨天","11-20","剛剛","昨天","11-20"};public static AlertDialog.Builder getAlertBuilder(Context context){AlertDialog.Builder builder = new AlertDialog.Builder(context);//標題builder.setTitle("**通訊信息**");//logobuilder.setIcon(R.mipmap.a);//設置數據源  List<? extends Map<String, ?>> dataList<Map<String, Object>> datas = new ArrayList<>();for(int i =0 ; i<arrImg.length;i++) {//各項數據Map<String, Object> itemData = new HashMap<>();itemData.put("item_img", arrImg[i]);itemData.put("item_title", arrTitle[i]);itemData.put("item_content", arrContent[i]);itemData.put("item_time", arrTime[i]);//添加到Listdatas.add(itemData);}//    第1個參數: Context
//    第2個參數: 數據 List<map<String,Object>> datas
//    第3個參數: xml資源文件 R.layout....
//    第4個參數: 控制從datas中取出哪些數據 與map中的 key一致
//    第5個參數: 布局文件的id  控制取出的數據要填充哪些界面元素。SimpleAdapter simpleAdapter = new SimpleAdapter(context,datas,R.layout.content_layout,new String[]{"item_img","item_title","item_content","item_time"},new int[]{R.id.btn_image_view,R.id.btn_title_view,R.id.btn_tv_content,R.id.btn_time});//內容 設置適配器builder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(context, "您選擇了"+which, Toast.LENGTH_SHORT).show();}});//取消builder.setPositiveButton("取消",null);return builder;}
}

主布局文件 activity_main.xml

<?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:orientation="vertical"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/btn_tv"android:text="您選擇了:"android:textSize="24sp"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/btn_one"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24sp"android:text="普通的AlertDialog"/><Buttonandroid:id="@+id/btn_Two"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24sp"android:text="單選AlertDialog"/><Buttonandroid:id="@+id/btn_three"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24sp"android:text="單選2AlertDialog"/><Buttonandroid:id="@+id/btn_four"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24sp"android:text="多選AlertDialog"/><Buttonandroid:id="@+id/btn_five"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24sp"android:text="適配器AlertDialog"/></LinearLayout>

適配器中的布局 content_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- RelativeLayout 相對布局--><ImageViewandroid:id="@+id/btn_image_view"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="10dp"/><!-- 標題在圖片的右邊--><TextViewandroid:layout_marginLeft="10dp"android:layout_toRightOf="@+id/btn_image_view"android:id="@+id/btn_title_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="22sp"android:textStyle="bold"/><!-- 畫條線在圖片的右邊在文字的下面--><TextViewandroid:id="@+id/btn_tv_line"android:layout_below="@+id/btn_title_view"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/btn_image_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ccc"android:minHeight="1dp"android:maxHeight="1dp"/><!-- 內容在圖片的右邊在線的下面--><TextViewandroid:layout_below="@+id/btn_tv_line"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/btn_image_view"android:id="@+id/btn_tv_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"/><!-- 時間提示在最右邊顯示在父元素內右邊--><TextViewandroid:id="@+id/btn_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_alignParentRight="true"/></RelativeLayout>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/165936.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/165936.shtml
英文地址,請注明出處:http://en.pswp.cn/news/165936.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【每日一題】2824. 統計和小于目標的下標對數目-2023.11.24

題目&#xff1a; 2824. 統計和小于目標的下標對數目 給你一個下標從 0 開始長度為 n 的整數數組 nums 和一個整數 target &#xff0c;請你返回滿足 0 < i < j < n 且 nums[i] nums[j] < target 的下標對 (i, j) 的數目。 示例 1&#xff1a; 輸入&#xff1…

雙12電視盒子什么牌子好?數碼小編力薦目前最強的電視盒子

最近想買電視盒子的網友非常多&#xff0c;小編收到了很多關于電視盒子方面的咨詢&#xff0c;因此我特意整理了今年測評過的電視盒子&#xff0c;總結了五款目前最強的電視盒子&#xff0c;想知道雙十二買電視盒子什么牌子好就趕緊收藏起來吧。 推薦一&#xff1a;泰捷WEBOX新…

01 LM 算法及 Cpp 實現

文章目錄 01 LM 算法及 Cpp 實現1.1 應用1.2 阻尼法推導1.3 Cpp 算法實現 01 LM 算法及 Cpp 實現 1.1 應用 LM 算法用于解決非線性最小二乘問題 min ? x F ( x ) 1 2 ∥ f ( x ) ∥ 2 2 (1) \min _x F(x)\frac{1}{2}\|f(\boldsymbol{x})\|_2^2 \tag{1} xmin?F(x)21?∥f(x…

代理模式 rust和java的實現

文章目錄 代理模式介紹實現javarust rust倉庫 代理模式 在代理模式&#xff08;Proxy Pattern&#xff09;中&#xff0c;一個類代表另一個類的功能。在代理模式中&#xff0c;我們創建具有現有對象的對象&#xff0c;以便向外界提供功能接口。 介紹 意圖&#xff1a;為其他對…

jquery中ajax總結

在ajax請求接口的時候&#xff0c;常用到以下參數&#xff0c;在這里記錄下 contentType用于告訴服務器請求的數據類型&#xff0c;常見的有 text/html&#xff1a;HTML網頁 text/plain&#xff1a;純文本 application/json&#xff1a;JSON格式 application/xml&#xff1a;XM…

“KeyarchOS:國產Linux新星的崛起與創新之路“

簡介 KeyarchOS是一款由浪潮信息自主研發的服務器操作系統。它因為幾個特點而受到我的青睞和一些用戶的關注。 首先&#xff0c;KeyarchOS注重安全性和穩定性。它有一些防護和隔離功能&#xff0c;來幫助系統穩定運行&#xff0c;而且是中文語言更接地氣。 其次&#xff0c;Ke…

OSG編程指南<十>:OSG幾何體的繪制

1、場景基本繪圖類 在 OSG 中創建幾何體的方法比較簡單&#xff0c;通常有 3 種處理幾何體的手段&#xff1a; 使用松散封裝的OpenGL 繪圖基元&#xff1b;使用 OSG 中的基本幾何體&#xff1b;從文件中導入場景模型。 使用松散封裝的OpenGL 繪圖基元繪制幾何體具有很強的靈活…

牛氣霸屏-快抖云推獨立版V1.6.7

介紹 快抖云推全插件獨立版是最近很火的牛氣霸屏系統獨立版&#xff0c;牛氣霸屏系統就是商家通過系統在線創建抖音或快手霸屏活動&#xff0c;并生成該活動的爆客二維碼&#xff0c;用戶通過掃二維碼即可參加活動&#xff08;活動可以是領取卡劵&#xff0c;抽獎等&#xff0…

DevExpress中文教程 - 如何在macOS和Linux (CTP)上創建、修改報表(下)

DevExpress Reporting是.NET Framework下功能完善的報表平臺&#xff0c;它附帶了易于使用的Visual Studio報表設計器和豐富的報表控件集&#xff0c;包括數據透視表、圖表&#xff0c;因此您可以構建無與倫比、信息清晰的報表。 DevExpress Reports — 跨平臺報表組件&#x…

linux(1)之build構建系統基礎(一)

Linux(1)之buildroot構建系統(一) Author&#xff1a;Onceday Date&#xff1a;2023年11月12日 漫漫長路&#xff0c;才剛剛開始… 參考文檔&#xff1a; The Yocto ProjectBuildroot - Making Embedded Linux Easy 文章目錄 Linux(1)之buildroot構建系統(一)1. 概述1.1 如…

企業數字化轉型轉什么?怎么轉?這份攻略請收好

目錄 -01-數字化轉型“是什么” -02-數據驅動推動企業數字化轉型 -03-企業數字化轉型的行動路線圖 數字化轉型&#xff0c;轉什么&#xff1f;怎么轉&#xff1f;這些問題仍在困擾不少企業&#xff0c;也是每個企業轉型升級不得不思考的重要問題。對此&#xff0c;中關村數字…

Python潮流周刊#1:如何系統地自學Python?

這里記錄每周值得分享的 Python 及通用技術內容&#xff0c;部分內容為英文&#xff0c;已在小標題注明。&#xff08;本期標題取自其中一則分享&#xff0c;不代表全部內容都是該主題&#xff0c;特此聲明。&#xff09; 文章&教程 1、編程語言的錯誤處理模式 文章討論…

requests請求django接口跨域問題處理

參考&#xff1a; https://zhuanlan.zhihu.com/p/416978320 https://blog.csdn.net/SweetHeartHuaZai/article/details/130983179 使用httpx代替requests import httpxheaders {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3…

銷售為什么會選擇使用電銷這種方式 ?

在網絡經濟時代的大環境下&#xff0c;網絡營銷作為一種新型營銷模式和營銷理念&#xff0c;已經搶占了大部分市場。 網絡營銷&#xff0c;是指利用互聯網技術和現代信息技術&#xff0c;以及社交媒體平臺&#xff0c;進行產品宣傳、銷售、服務、品牌傳播等活動的一種營銷模式。…

MySQL-進階

存儲引擎 MySQL體系結構 連接層&#xff1a; 最上層是一些客戶端和連接服務&#xff0c;主要完成一些類似于連接處理、授權認證、及相關的安全方案。服務器也會為安全接入的每個客戶端驗證它所具有的操作權限。服務層&#xff1a; 第二層架構主要完成大多數的核心服務功能&…

財報解讀:三季度的美國零售,“沃爾瑪效應”仍在持續

經濟學中常用“沃爾瑪效應”來指代“消費者減少消費時&#xff0c;會選擇每種類別中價格最低的商品”這一現象。作為全球最大的零售商&#xff0c;沃爾瑪一定程度上成為了消費市場的風向標。 近日&#xff0c;沃爾瑪發布的2024財年第三季度財報顯示&#xff0c;其相較去年同期…

虛擬機(Linux)系統知識普及:什么是Linux發行版 以及各發行版的區別

什么是Linux發行版 以及各發行版的區別 一. 什么是linux發行版簡單來說正式定義區別資料1區別資料2區別資料3區別資料4注意事項二. Linux發行版:CentOS、Ubuntu、RedHat、Android、Tizen、MeeGoLinux 發行版:RedhatDebianUbuntuGentooFreeBSD

22款奔馳S400L升級主動式氛圍燈 光影彰顯奔馳的完美

新款奔馳S級原車自帶64色氛圍燈&#xff0c;還可以升級原廠的主動式氛圍燈&#xff0c;增加車內的氛圍效果。主動式環境氛圍燈包含263個LED光源&#xff0c;每隔1.6厘米就有一個LED光源&#xff0c;照明效果較過去明亮10倍&#xff0c;視覺效果更加絢麗&#xff0c;它還可結合智…

Python中的下劃線使用教程:單下劃線、雙下劃線和頭尾雙下劃線詳解

概要 Python是一種簡單、易學、功能強大的編程語言&#xff0c;被廣泛應用于各種領域。在Python中&#xff0c;下劃線的使用有其特殊的含義和用途。本文將詳細介紹Python中的單下劃線、雙下劃線和頭尾雙下劃線的使用教程&#xff0c;幫助讀者更好地理解和應用這些特性。 一、單…

干貨!ERP軟件如何幫助企業實現信息化管理?

ERP即企業資源規劃&#xff08;Enterprise Resource Planning&#xff09;系統&#xff0c;其核心是物料的追蹤流轉。而在物料追蹤流轉的基礎上&#xff0c;又衍生出一系列各類資源計劃的管理和追蹤。因此ERP發展成為一款集成各類資源計劃&#xff0c;也就是集成企業核心業務流…