【发布时间】:2018-05-24 09:19:26
【问题描述】:
我有一个添加了编辑文本的警报对话框。它有正面和负面的按钮。每当我单击肯定/确定按钮时,即使编辑文本为空,对话框也会退出。
我想控制这种行为。即;我希望仅当编辑文本不为空时才退出警报对话框。
注意:strAuthorDesc 是一个字符串变量。
这是单击按钮时执行的代码。
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle("Author Desc.");
final EditText descInput = new EditText(mActivity);
descInput.setInputType(InputType.TYPE_CLASS_TEXT);
descInput.setTextColor(ContextCompat.getColor(mActivity, R.color.black));
descInput.setLines(5);
// descInput.setMaxLines(5);
descInput.setGravity(Gravity.LEFT | Gravity.TOP);
descInput.setSingleLine(false);
descInput.setHorizontalScrollBarEnabled(false);
builder.setView(descInput);
builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
strAuthorDesc = descInput.getText().toString();
if(strAuthorDesc != null && !strAuthorDesc.equals("")){
dialog.dismiss();
Const.showToast("Description added", mActivity);
setAuthorDesc();
}else{
Const.showToast("Desc. cannot be empty!", mActivity);
}
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog = builder.create();
alertDialog.setCancelable(false);
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
【问题讨论】:
标签: android android-alertdialog