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