我有一个AlertDialog,其视图如下所示。
userEmail = new EditText(mActivity);
userEmail.setSingleLine(true);
userEmail.setHint("Email address");
userEmail.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
userEmail.addTextChangedListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(getResources().getString(R.string.text_dialog_email));
builder.setView(userEmail);
我已经为事件监听器添加了一个POSITIVE按钮。
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(userEmail.getText().toString().trim().length() <= 0) {
userEmail.setError("Email address can't be empty");
} else {
//some logic
InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(userEmail.getWindowToken(), 0);
dialog.dismiss();
}
} });
builder.create();
builder.show();
当用户点击确定按钮时,会有电子邮件验证。如果Edittext为空,我希望对话框保持不变。但是,实际上,即使EditText为空,对话框也会消失。
我缺少什么?
修改
整个代码位于一个方法openEmailDialog()中。它是在按钮上调用的。
@Override
public void onClick(View view) {
openEmailDialog();
}
答案 0 :(得分:0)
如果您通过构建器添加onClickListener
,则对话框将自动关闭。
您可以通过手动将View.OnClickListener
添加到对话框的Button
视图来阻止此操作。显示后,您可以从对话框中收到Button
View
。
你应该试试这个:
builder.setPositiveButton(android.R.string.ok, null);//init button
final AlertDialog d = builder.create();//store reference to the dialog
//the click listener for your button
final View.OnClickListener myListener = new View.OnClickListener(){
public void onClick(View v){
if(userEmail.getText().toString().trim().length() <= 0) {
userEmail.setError("Email address can't be empty");
} else {
//some logic
InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(userEmail.getWindowToken(), 0);
d.dismiss();
}
}
});
d.setOnShowListener(new DialogInterface.OnShowListener(){
public void onShow(DialogInterface dialog){
//here get the Button and set onclicklistener
Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
b.setOnClickListener(myListener);//your validation in your onclicklistener.
//do not forget to dismiss the dialon in your View.OnClickListener
}
});
d.show();
答案 1 :(得分:0)
您可以使用Java Reflection解决此问题。
private void canCloseDialog(DialogInterface dialogInterface, boolean close) {
try {
Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialogInterface, close);
} catch (Exception e) {
e.printStackTrace();
}
}
在onClick回调中调用此方法
new AlertDialog.Builder(this)
.setTitle(R.string.abc)
.setView(passwdEditText)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
canCloseDialog(dialogInterface, false);//will NOT close the Dialog
}
})
.setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
canCloseDialog(dialogInterface, true);//will close the Dialog
}
})
.show();