欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Studio實現(xiàn)單選對話框

 更新時間:2022年05月17日 10:39:57   作者:言人冰  
這篇文章主要為大家詳細介紹了Android Studio實現(xiàn)單選對話框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android Studio實現(xiàn)單選對話框的具體代碼,供大家參考,具體內(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="單選對話框"
? ? ? ? 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};//存儲字體大小
? ? private ?String[] fontStyleArr= {"小號","默認","中號","大號","超大"};//存儲樣式
? ? int textSize = 1; //單選列表中默認選擇的位置
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //設(shè)置監(jiān)聽
? ? ? ? findViewById(R.id.btn).setOnClickListener(this); //為id為btn的按鈕邦定監(jiān)聽
? ? ? ? textView = (TextView) findViewById(R.id.tv);

? ? }

? ? @Override
? ? public void onClick(View view) {
? ? ? ? // ?創(chuàng)建對話框并設(shè)置其樣式(這里采用鏈式方程)
? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)//設(shè)置單選框列表
? ? ? ? ? ? ? ? .setTitle("設(shè)置字體的大小") ? //設(shè)置標題
? ? ? ? ? ? ? ? .setIcon(R.drawable.bdd) //設(shè)置圖標
? ? ? ? ? ? ? ? .setSingleChoiceItems(fontStyleArr, textSize, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? textSize=i; //在OnClick方法中得到被點擊的序號 i
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() {//在對話框中設(shè)置“確定”按鈕
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? //為TextView設(shè)置在單選對話框中選擇的字體大小
? ? ? ? ? ? ? ? ? ? ? ? textView.setTextSize(textSizeArr[textSize]);
? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置好字體大小后關(guān)閉單選對話框
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {//在對話框中設(shè)置”取消按鈕“
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? dialog = builder.create();
? ? ? ? dialog.show();
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論