If EditText.getText().ToString() == “” dont work;

2019-01-20 20:32发布

This question already has an answer here:

Here is my code:

if(editText.getText().toString() == ""){
   editTextBenefaction.setText("0");
}

Why he didn't work?

2条回答
We Are One
2楼-- · 2019-01-20 20:44

Do not use == with Strings use equals()

if(editText.getText().toString().equals("")){
   editTextBenefaction.setText("0");
}
查看更多
3楼-- · 2019-01-20 20:56

Change it to

if(editText.getText().toString().equals("")){

In Java .equals() is used to compare if they have the same value and "==" is used to determine if they reference the same object.

An even better way is to use

if("".equals(editText.getText().toString())){

because this will protect against a NPE.

Java String Docs

查看更多
登录 后发表回答