日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

024 Android 自定义样式对话框(AlertDialog)

發(fā)布時間:2024/9/5 Android 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 024 Android 自定义样式对话框(AlertDialog) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.AlertDialog介紹

AlertDialog并不需要到布局文件中創(chuàng)建,而是在代碼中通過構(gòu)造器(AlertDialog.Builder)來構(gòu)造標題、圖標和按鈕等內(nèi)容的。

常規(guī)使用步驟(具體參見Android 開發(fā)博客中的024篇):

(1)創(chuàng)建構(gòu)造器AlertDialog.Builder的對象;
(2)通過構(gòu)造器的對象調(diào)用setTitle、setMessage等方法構(gòu)造對話框的標題、信息和圖標等內(nèi)容;
(3)根據(jù)需要,設(shè)置正面按鈕、負面按鈕和中立按鈕;
(4)調(diào)用create方法創(chuàng)建AlertDialog的對象;
(5)AlertDialog的對象調(diào)用show方法,讓對話框在界面上顯示。

只顯示簡單的標題和信息是滿足不了我們的要求,比如我們要實現(xiàn)一個登錄對話框的話,那就需要在對話框上放置EditText輸入框了。AlertDialog早就為我們準備好了setView方法,只要往里面放進我們需要顯示的View對象就可以了。

2.自定義對話框

(1)xml頁面布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--android:background="#fcc" 會覆蓋掉style中原本設(shè)置的background屬性值--><TextViewstyle="@style/TitleStyle"android:background="#fcc"android:text="添加黑名單號碼" /><EditTextandroid:id="@+id/et_black_phone_call"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#000"android:hint="請輸入攔截號碼" /><RadioGroupandroid:id="@+id/rg_black_call"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><!--android:checked="true" 默認選中條目--><RadioButtonandroid:id="@+id/rb_sms"android:text="短信"android:textColor="#000"android:checked="true"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb_phone"android:text="電話"android:textColor="#000"android:layout_marginLeft="15dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb_all"android:text="所有"android:textColor="#000"android:layout_marginLeft="15dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RadioGroup><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/bt_black_confirm"android:layout_width="0dp"android:text="確認"android:textColor="#000"android:background="@drawable/selector_black_call_btn_bg"android:layout_height="wrap_content"android:layout_weight="1"/><Buttonandroid:id="@+id/bt_black_cancel"android:layout_width="0dp"android:text="取消"android:textColor="#000"android:background="@drawable/selector_black_call_btn_bg"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout> </LinearLayout>

(2)java后臺代碼

private void showDialog() {//采用自定義的對話框樣式,利用dialog.setView(view);AlertDialog.Builder builder=new AlertDialog.Builder(this);final AlertDialog dialog=builder.create();final View view=View.inflate(this,R.layout.dialog_set_black_call_list,null);dialog.setView(view);dialog.show();//顯示對話框//找到自定義對話框布局文件中的控件final EditText et_black_phone_call=view.findViewById(R.id.et_black_phone_call);RadioGroup radioGroup=view.findViewById(R.id.rg_black_call);Button bt_black_confirm=view.findViewById(R.id.bt_black_confirm);Button bt_black_cancel=view.findViewById(R.id.bt_black_cancel);//監(jiān)聽radioGroup選中條目的切換過程radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.rb_sms:function_type="攔截短信";break;case R.id.rb_phone:function_type="攔截電話";break;case R.id.rb_all:function_type="攔截所有";break;}}});bt_black_confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//1.獲取輸入框的電話號碼String phoneNumber=et_black_phone_call.getText().toString();if(!TextUtils.isEmpty(phoneNumber)){//2.向數(shù)據(jù)庫中插入當前用戶輸入的攔截號碼 BlackListCallDBUtil.insertOneRecord(phoneNumber,function_type);initData();}else {Toast.makeText(getApplicationContext(),"請輸入電話號碼",Toast.LENGTH_SHORT).show();}}});bt_black_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss(); //關(guān)閉對話框 }});}

3.效果圖

轉(zhuǎn)載于:https://www.cnblogs.com/luckyplj/p/10847912.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的024 Android 自定义样式对话框(AlertDialog)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。