Android Studio實(shí)現(xiàn)單選對(duì)話框
本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)單選對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
上效果圖

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:layout_height="match_parent" ? ? tools:context=".MainActivity" ? ? android:orientation="vertical" ? ? > ? ? <TextView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="單選對(duì)話框" ? ? ? ? android:textSize="20sp" ? ? ? ? android:layout_marginTop="30dp" ? ? ? ? android:gravity="center" ? ? ? ? android:id="@+id/tv" ? ? ? ? /> ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="設(shè)置字體大小" ? ? ? ? android:id="@+id/btn" ? ? ? ? android:layout_marginTop="20dp" ? ? ? ? android:layout_gravity="center" ? ? ? ? /> </LinearLayout>
MainActivity.java
package com.example.singlechoicedialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
? ? private AlertDialog dialog;
? ? private TextView textView;
? ? private int[] textSizeArr = {10,20,25,30,40};//存儲(chǔ)字體大小
? ? private ?String[] fontStyleArr= {"小號(hào)","默認(rèn)","中號(hào)","大號(hào)","超大"};//存儲(chǔ)樣式
? ? int textSize = 1; //單選列表中默認(rèn)選擇的位置
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //設(shè)置監(jiān)聽(tīng)
? ? ? ? findViewById(R.id.btn).setOnClickListener(this); //為id為btn的按鈕邦定監(jiān)聽(tīng)
? ? ? ? textView = (TextView) findViewById(R.id.tv);
? ? }
? ? @Override
? ? public void onClick(View view) {
? ? ? ? // ?創(chuàng)建對(duì)話框并設(shè)置其樣式(這里采用鏈?zhǔn)椒匠?
? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)//設(shè)置單選框列表
? ? ? ? ? ? ? ? .setTitle("設(shè)置字體的大小") ? //設(shè)置標(biāo)題
? ? ? ? ? ? ? ? .setIcon(R.drawable.bdd) //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setSingleChoiceItems(fontStyleArr, textSize, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? textSize=i; //在OnClick方法中得到被點(diǎn)擊的序號(hào) i
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() {//在對(duì)話框中設(shè)置“確定”按鈕
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? //為T(mén)extView設(shè)置在單選對(duì)話框中選擇的字體大小
? ? ? ? ? ? ? ? ? ? ? ? textView.setTextSize(textSizeArr[textSize]);
? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置好字體大小后關(guān)閉單選對(duì)話框
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {//在對(duì)話框中設(shè)置”取消按鈕“
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? dialog = builder.create();
? ? ? ? dialog.show();
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android studio自定義對(duì)話框效果
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android?Studio使用自定義對(duì)話框效果
相關(guān)文章
Android系統(tǒng)對(duì)話框使用詳解(最詳細(xì))
這篇文章主要介紹了Android系統(tǒng)對(duì)話框使用詳解(最詳細(xì)),需要的朋友可以參考下2017-10-10
Android 實(shí)現(xiàn)旋轉(zhuǎn)木馬的音樂(lè)效果
大家一定在百度音樂(lè)上在線聽(tīng)過(guò)歌,有沒(méi)有注意到那個(gè)旋轉(zhuǎn)的唱片,本篇文章主要介紹在Android上如何實(shí)現(xiàn)這樣的功能,有需要的小伙伴可以參考下2016-07-07
Android的ListView多選刪除操作實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android的ListView多選刪除操作實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
android使用TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了android使用TextView實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn)
本篇文章主要介紹了Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理
本篇文章主要介紹了android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android布局中margin與padding的區(qū)別及說(shuō)明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單
這篇文章主要為大家詳細(xì)介紹了Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
初步編寫(xiě)IDEA\AndroidStudio翻譯插件的方法
這篇文章主要介紹了初步編寫(xiě)IDEA\AndroidStudio翻譯插件,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

