Determine whether the input box EditText is empty in Android
Method 1: Judging by the method TextUtils.isEmpty(s), the parameter s is the content of the input box obtained
Method 2: Through the method str.equals(""), str is the content of the obtained input box
Simply test these two methods with the following piece of code
public class MainActivity extends AppCompatActivity { EditText edit; Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.edit); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String s = edit.getText().toString().trim(); if(TextUtils.isEmpty(s)){ Toast.makeText(getApplicationContext(), " Method 1: The input box is empty " ,Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(), "The input box is not empty " ,Toast.LENGTH_LONG).show(); } } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String s = edit.getText().toString().trim(); if(s.equals("")){ Toast.makeText(getApplicationContext(), " Method 2: The input box is empty " ,Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(), "The input box is not empty " ,Toast.LENGTH_LONG).show(); } } }); } }
Effect picture: