要求用户填写空白“EditText”的Toast消息领域
我试图在单击按钮时显示一条消息,告诉用户填写空白字段。目前,如果该字段为空,则会崩溃/强制关闭应用程序。我尝试执行以下代码,但成功率为零。最初我没有 if/else 在那里,我只是运行 Calculator();方法和下面的imm代码。
有人能指出我正确的方向吗?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
我很确定这是糟糕的代码:
if ((EditText)findViewById(R.id.amount1)== null)
只是不知道如何按照我想要的方式表达它。
I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. I tried to do the following code and had zero success. Originally I didn't have the if/else in there, I just ran the calculator(); method and the following imm code.
Could someone point me into the right direction?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im pretty sure this is the bad code:
if ((EditText)findViewById(R.id.amount1)== null)
Just dont know how to word it the way I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试检查 EditText 小部件中文本的长度
Try checking the length of the text in the EditText widget
使用此代码。
Use this code.
即使该字段为空,edittext 也不为空。使用:
Even if the field is blank, the edittext is not null. Use:
((EditText)findViewById(R.id.amount1)== null
只是获取对 ID 为amount1
的 EditText 的引用,它不会检查是否存在 要查看 EditText 是否有文本,您可以通过 EditText#getText().toString() 获取它所保存的字符串。
要实现此操作,首先存储对EditText 在 var 中,然后对字符串执行检查:
我正在使用局部变量,并且假设您希望字符串具有内容,您可能需要执行其他检查来处理所有情况(例如格式错误的输入)。您可以通过在 EditText 上设置 inputType 来减少这种情况,例如,如果您尝试仅处理十进制数字,则可以将其设置为 numberDecimal。
((EditText)findViewById(R.id.amount1)== null
is just getting a reference to the EditText with the idamount1
, it is not checking to see if that EditText has a valid entry.To see if the EditText has text, you can get the String it holds by via
EditText#getText().toString()
To make this work, first store the reference to the EditText in a var, then perform your checks on the String:
I'm using local variables and just assuming you want the string to have content. You will likely need to do other checks to handle all the cases (like malformed input). Some of this you can reduce by setting the inputType on the EditText. For example, you might set it to numberDecimal if you are trying to handle only decimal numbers.
您实际上想要检查 EditText 的内容是否为空或空字符串。
有问题的行应该看起来像这样:
当然,您可能希望将该语句分成更多行,以使其更具可读性!
You actually want to check if the contents of the EditText are null or an empty string.
The line in question should look something like this:
Of course you may want to break that statement up into more lines to make it a bit more readable!