要求用户填写空白“EditText”的Toast消息领域

发布于 2024-11-08 12:26:49 字数 852 浏览 0 评论 0原文

我试图在单击按钮时显示一条消息,告诉用户填写空白字段。目前,如果该字段为空,则会崩溃/强制关闭应用程序。我尝试执行以下代码,但成功率为零。最初我没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

俏︾媚 2024-11-15 12:26:49

尝试检查 EditText 小部件中文本的长度

EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}

Try checking the length of the text in the EditText widget

EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
や三分注定 2024-11-15 12:26:49

使用此代码。

EditText et = (EditText)findViewById(R.id.amount1));

if(et1.getText().length() == 0){
    //Display toast here
} else{
    //Your code
}

Use this code.

EditText et = (EditText)findViewById(R.id.amount1));

if(et1.getText().length() == 0){
    //Display toast here
} else{
    //Your code
}
旧城空念 2024-11-15 12:26:49
EditText text = (EditText)findViewById(R.id.amount1);

if(TextUtils.isEmpty(text.toString())) {
    // show toast
}
EditText text = (EditText)findViewById(R.id.amount1);

if(TextUtils.isEmpty(text.toString())) {
    // show toast
}
开始看清了 2024-11-15 12:26:49

即使该字段为空,edittext 也不为空。使用:

EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());

if (test.equals("")) {
//...

Even if the field is blank, the edittext is not null. Use:

EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());

if (test.equals("")) {
//...
今天小雨转甜 2024-11-15 12:26:49

((EditText)findViewById(R.id.amount1)== null 只是获取对 ID 为 amount1 的 EditText 的引用,它不会检查是否存在 要查看 EditText 是否有文本

,您可以通过 EditText#getText().toString() 获取它所保存的字符串。

要实现此操作,首先存储对EditText 在 var 中,然后对字符串执行检查:

EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();

if (amount1.equals("")) {
  // Do your stuff here
}

我正在使用局部变量,并且假设您希望字符串具有内容,您可能需要执行其他检查来处理所有情况(例如格式错误的输入)。您可以通过在 EditText 上设置 inputType 来减少这种情况,例如,如果您尝试仅处理十进制数字,则可以将其设置为 numberDecimal。

((EditText)findViewById(R.id.amount1)== null is just getting a reference to the EditText with the id amount1, 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:

EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();

if (amount1.equals("")) {
  // Do your stuff here
}

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.

画尸师 2024-11-15 12:26:49

您实际上想要检查 EditText 的内容是否为空或空字符串。

有问题的行应该看起来像这样:

if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))

当然,您可能希望将该语句分成更多行,以使其更具可读性!

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:

if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))

Of course you may want to break that statement up into more lines to make it a bit more readable!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文