开发者

Toast message requiring user to fill in blank "EditText" fields

开发者 https://www.devze.com 2023-03-06 09:43 出处:网络
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 follow

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();
      Input开发者_StackOverflow社区MethodManager 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.


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
}


Use this code.

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

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


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

if(TextUtils.isEmpty(text.toString())) {
    // show toast
}


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("")) {
//...


((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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消