1.結果
2.プロセス
2.1。レイアウトファイルalert_sign.xmlを作成します(スペルミスもあります...気にしないでください)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:textAlignment="textEnd"
android:paddingEnd="10dp"/>
<EditText
android:id="@+id/edt_usr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入用户名"
android:textAlignment="viewStart" />
</TableRow>
<TableRow>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:textAlignment="textEnd"
android:paddingEnd="10dp"/>
<EditText
android:id="@+id/edt_pas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入密码"
android:inputType="textPassword"
android:textAlignment="viewStart" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="确认信息" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="取消登录" />
</LinearLayout>
</LinearLayout>
2.2。使用
View.OnClickListener listener;
AlertDialog dialog;
public void alertSignTable() {
LayoutInflater fac = LayoutInflater.from(ShaActivity.this);
View alertView = fac.inflate(R.layout.alert_sign, null);
dialog = new AlertDialog.Builder(ShaActivity.this)
.setTitle("登录弹框")
.setView(alertView)
.create();
dialog.show();
Button btn_confirm = dialog.getWindow().findViewById(R.id.btn_confirm);
Button btn_cancel = dialog.getWindow().findViewById(R.id.btn_cancel);
btn_confirm.setOnClickListener(listener);
btn_cancel.setOnClickListener(listener);
}
listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sign:
alertSignTable();
break;
case R.id.btn_cancel:
dialog.dismiss();
break;
case R.id.btn_confirm:
EditText edt_usr = dialog.getWindow().findViewById(R.id.edt_usr);
EditText edt_pas = dialog.getWindow().findViewById(R.id.edt_pas);
TextView tv_login = findViewById(R.id.tv_login);
String usr = edt_usr.getText().toString();
String pas = edt_pas.getText().toString();
if ("root".equals(usr) && "root".equals(pas)) {
tv_login.setText("通过");
}else {
tv_login.setText("错误");
}
dialog.dismiss();
break;
default:
break;
}
}
};
btn_sign.setOnClickListener(listener);
3.まとめ
1. 获取弹框的组件方法是dialog.getWindow().findViewById(R.id);
2. 不能再setPositiveButton中设置获取弹框组件,会获取到空值,原因应该与dialog创建方式有关。