第三類
建立 Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/edittext"
/>
</LinearLayout>
在程式中載入 Layout
LayoutInflater inflater = LayoutInflater.from(AlertDialogDemoActivity.this);
final View v = inflater.inflate(R.layout.alertdialog_edittext, null);
把AlertDialog丟進去這個View
new AlertDialog.Builder(AlertDialogDemoActivity.this)
.setTitle("請輸入你的名字")
.setView(v)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText editText = (EditText) (v.findViewById(R.id.edittext));
Toast.makeText(getApplicationContext(), "你叫做"+ editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
})
.show();
AlertDialog 屬性
Builder
setTitle
setView
setPositiveButton
建立視窗
建立標題
建立顯示的 Layout
第一個按鈕
第一個參數
第二個參數
按鈕文字
指定 click Listener