0
我想创建一个AlertDialog来检查两个密码是否匹配。问题是,当我从布局中充填视图时,对话框不会显示,OK和Cancel按钮也不会显示只有EditText上显示,并且太在Mainactivity.This是使用自定义对话框我用这样far.Could有人帮我这个Alertdialog无法正常显示视图
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case MY_PASSWORD_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.activity_main, (ViewGroup) findViewById(R.id.root));
final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Enter Password");
builder.setView(layout);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String strPassword1 = password1.getText().toString();
String strPassword2 = password2.getText().toString();
if (strPassword1.equals(strPassword2)) {
Toast.makeText(MainActivity.this,
"Matching passwords="+strPassword2, Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}
return null;
}
似乎不工作...你能请编辑我的代码,所以我可以看到,如果我做错了 – user2429689
只需复制并粘贴此代码使用你的布局和你的小部件 –
它说“类型View中的方法setOnClickListener(View.OnClickListener)不适用于参数(new OnClickListener(){}); – user2429689