无法检查 AlertDialog.Builder 中 EditText 的值
我试图检查 AlertDialog.Builder 中 EditText 的值,以便防止用户在 EditText 中输入任何内容。
由于某种原因,当我使用下面的代码时,它永远不会进入 if (mEditText.getText() == null || test == "") 语句,它总是执行 else 语句。
有什么想法吗?谢谢各位。
@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(mContext);
final View textEntryView = factory.inflate(R.layout.file_save_dialog, null);
mEditText = (EditText) textEntryView.findViewById(R.id.file_name_edit);
final Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
return new AlertDialog.Builder(mContext)
.setIcon(android.R.drawable.ic_menu_save)
.setTitle(R.string.file_name_title)
.setView(textEntryView)
.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String test = mEditText.getText().toString();
//This if never works...it always hits the else statement
if (mEditText.getText() == null || test == "") {
mEditText.startAnimation(shake);
}
else {
finish();
}
}
})
.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.create();
}
I'm trying to check the value of the EditText inside my AlertDialog.Builder so that I can prevent the user from entering nothing in the EditText.
For some reason when I use the code below it never gets into the if (mEditText.getText() == null || test == "")
statement, it always executes the else statement instead.
Any ideas? Thanks folks.
@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(mContext);
final View textEntryView = factory.inflate(R.layout.file_save_dialog, null);
mEditText = (EditText) textEntryView.findViewById(R.id.file_name_edit);
final Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
return new AlertDialog.Builder(mContext)
.setIcon(android.R.drawable.ic_menu_save)
.setTitle(R.string.file_name_title)
.setView(textEntryView)
.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String test = mEditText.getText().toString();
//This if never works...it always hits the else statement
if (mEditText.getText() == null || test == "") {
mEditText.startAnimation(shake);
}
else {
finish();
}
}
})
.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.create();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照Android方式进行:
Do it in the Android way:
这样说
put it like this
如果您想比较两个字符串值,请使用方法
equals()
。例如:
If you want compare two String values, use method
equals()
.For example: