Android Dialog, keep dialog open when button is pressed?
目标:
如图
点击对话框中确定按钮之后需要判断输入是否为空,如果为空,显示toast,如果输入符合要求,那么dismiss掉对话框
参考链接:stackoverflow
代码如下:
final EditText etUserName = new EditText(context);
mChangeNameDialog = new AlertDialog.Builder(context)
.setTitle(ResourceUtils.getString(R.string.change_nick_name))
.setView(etUserName)
.setNegativeButton(ResourceUtils.getString(R.string.cancle), null)
.setPositiveButton(ResourceUtils.getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
mChangeNameDialog.show();
mChangeNameDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = StringUtils.toStrValue(etUserName.getText());
//判断是否为null或空字符
if (StringUtils.isBlank(userName)) {
view.showToast(ResourceUtils.getString(R.string.please_input_nick_name));
return;
}
//判断昵称是否含有空格
if (StringUtils.hasWhiteSpace(userName)) {
view.showToast(ResourceUtils.getString(R.string.has_white_space));
return;
}
dismissDialog();
//do something
}
});