public class MainActivity extends Activity {
EditText editText1, editText2, editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//自定義View對話框
public void customView(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("自定義View對話框")
.setView(R.layout.login)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (TextUtils.isEmpty(editText1.getText().toString()) ||
TextUtils.isEmpty(editText2.getText().toString()) ||
TextUtils.isEmpty(editText3.getText().toString())) {
Toast.makeText(MainActivity.this, "請填寫完整", Toast.LENGTH_SHORT).show();
//不滿足條件,“確定”按鈕無效
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.d("acccc", editText1.getText().toString());
Log.d("acccc", editText2.getText().toString());
Log.d("acccc", editText2.getText().toString());
//滿足條件,點擊“確定”後消失
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
//以下三行必須放在dialog.show();下面
editText1 = (EditText)dialog.findViewById(R.id.editText1);
editText2 = (EditText)dialog.findViewById(R.id.editText2);
editText3 = (EditText)dialog.findViewById(R.id.editText3);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- 定義一個普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義View對話框"
android:onClick="customView"
/>
</LinearLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
android:id="@+id/loginLayout"
>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用戶名:"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入登錄賬號"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密碼:"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請填寫密碼"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電話號碼:"/>
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請填寫您的電話號碼"
android:password="true"
/>
</TableRow>
</TableLayout>