1.AlertDialog介紹
AlertDialog並不需要到布局文件中創建,而是在代碼中通過構造器(AlertDialog.Builder)來構造標題、圖標和按鈕等內容的。
常規使用步驟(具體參見Android 開發博客中的024篇):
(1)創建構造器AlertDialog.Builder的對象;
(2)通過構造器的對象調用setTitle、setMessage等方法構造對話框的標題、信息和圖標等內容;
(3)根據需要,設置正面按鈕、負面按鈕和中立按鈕;
(4)調用create方法創建AlertDialog的對象;
(5)AlertDialog的對象調用show方法,讓對話框在界面上顯示。
只顯示簡單的標題和信息是滿足不了我們的要求,比如我們要實現一個登錄對話框的話,那就需要在對話框上放置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中原本設置的background屬性值--> <TextView style="@style/TitleStyle" android:background="#fcc" android:text="添加黑名單號碼" /> <EditText android:id="@+id/et_black_phone_call" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000" android:hint="請輸入攔截號碼" /> <RadioGroup android:id="@+id/rg_black_call" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <!--android:checked="true" 默認選中條目--> <RadioButton android:id="@+id/rb_sms" android:text="短信" android:textColor="#000" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb_phone" android:text="電話" android:textColor="#000" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb_all" android:text="所有" android:textColor="#000" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android: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"/> <Button android: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); //監聽radioGroup選中條目的切換過程 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public 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() { @Override public void onClick(View v) { //1.獲取輸入框的電話號碼 String phoneNumber=et_black_phone_call.getText().toString(); if(!TextUtils.isEmpty(phoneNumber)){ //2.向數據庫中插入當前用戶輸入的攔截號碼 BlackListCallDBUtil.insertOneRecord(phoneNumber,function_type); initData(); }else { Toast.makeText(getApplicationContext(),"請輸入電話號碼",Toast.LENGTH_SHORT).show(); } } }); bt_black_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); //關閉對話框 } }); }
3.效果圖