1首先使用的是new AlertDialog.Builder(this);对象而不是AlertDialog
2AlertDialog上的布局需要调用View.inflate(this, R.layout.name_pw, null);进行初始化
代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View inflate = View.inflate(this, R.layout.name_pw, null);
builder.setTitle("请输入用户名和密码")
.setView(R.layout.name_pw)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.e("wy", "onClick: "+this.toString()+ " 88: "+MainActivity.this.toString());
edtUser = (EditText) inflate.findViewById(R.id.edt_user);
edtPw = (EditText) inflate.findViewById(R.id.edt_pw);
Log.e("wy", "use: "+edtUser.getText().toString()+
" pw:"+edtPw.getText().toString() );
}
})
.setNegativeButton("取消", null);
builder.show();
name_pw.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edt_user"
android:text="xiaoming"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edt_pw"
android:text="123456"
android:password="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>