本示例功能為點擊按鈕贿条,彈出對話框,自定義AlertDialog的布局View重付,獲取輸入框EditText中的內(nèi)容,點擊確定按鈕以及空白處不關(guān)閉對話框凫乖。
- 創(chuàng)建布局文件确垫,設(shè)置按鈕的點擊事件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mazaiting.alertdialogtest.MainActivity"
>
<Button
android:onClick="open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示對話框"
/>
</RelativeLayout>
主頁面Activity的初始配置代碼
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 按鈕打開事件
*/
public void open(View view){
}
}
- 接下來的代碼都寫在open(View view)這個函數(shù)中帽芽。首先創(chuàng)建出AlertDialog的布局文件删掀,如下:
<?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:gravity="center"
android:layout_margin="50dp"
android:padding="15dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tip"
android:gravity="center"
android:text="請輸入文字"
android:textColor="@android:color/black"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:textSize="16sp"
android:layout_marginTop="20dp"
android:id="@+id/et_number"
android:hint="請輸入文字"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- 現(xiàn)在open(View view)方法中,加載出布局导街。
// 加載布局
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_id,null);
EditText et_number = (EditText) dialogView.findViewById(R.id.et_number);
- 創(chuàng)建AlertDialog披泪, 并實現(xiàn)其相應設(shè)置
/**
* 按鈕打開事件
*/
public void open(View view){
// 加載布局
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_id,null);
final EditText editText = (EditText) dialogView.findViewById(R.id.et_number);
new AlertDialog.Builder(this) // 使用android.support.v7.app.AlertDialog
.setView(dialogView) // 設(shè)置布局
.setCancelable(false) // 設(shè)置點擊空白處不關(guān)閉
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
String text = editText.getText().toString();
if(TextUtils.isEmpty(text)){ // 判斷輸入的內(nèi)容是否為空
setDialogIsShowing(dialog, false); // 設(shè)置不關(guān)閉
Toast.makeText(MainActivity.this, "內(nèi)容不能為空", Toast.LENGTH_SHORT)
.show();
}else{
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT)
.show();
}
}
}) // 設(shè)置確定按鈕,并設(shè)置監(jiān)聽事件})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
setDialogIsShowing(dialog, true); // 設(shè)置關(guān)閉
}
}) // 設(shè)置取消按鈕搬瑰,并設(shè)置監(jiān)聽事件
.create() // 創(chuàng)建對話框
.show(); // 顯示對話框
}
- 設(shè)置是否關(guān)閉對話框款票,函數(shù)內(nèi)容:
/**
* 設(shè)置對話框是否顯示
* @param dialog 對話框
* @param isClose 是否顯示. true為關(guān)閉控硼,false為不關(guān)閉
*/
private void setDialogIsShowing(DialogInterface dialog, boolean isClose) {
try {
// 獲取到android.app.Dialog類
Field mShowing = dialog.getClass().getSuperclass().getSuperclass().getDeclaredField("mShowing");
mShowing.setAccessible(true); // 設(shè)置可訪問
mShowing.set(dialog,isClose); // 設(shè)置是否關(guān)閉
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
- 點擊空白處不關(guān)閉對話框,應添加
.setCancelable(false) // 設(shè)置點擊空白處不關(guān)閉
其中false為不關(guān)閉艾少,true為關(guān)閉卡乾。