安卓如何判断输入框不为空
时间: 2024-05-12 22:20:38 浏览: 15
在安卓中,可以通过以下方式判断输入框是否为空:
1. 使用TextUtils类中的isEmpty()方法判断字符串是否为空。
```
EditText editText = findViewById(R.id.edit_text);
if (TextUtils.isEmpty(editText.getText().toString())) {
// 输入框为空
} else {
// 输入框不为空
}
```
2. 使用String类中的trim()方法去除空格后再判断字符串是否为空。
```
EditText editText = findViewById(R.id.edit_text);
if (editText.getText().toString().trim().equals("")) {
// 输入框为空
} else {
// 输入框不为空
}
```
3. 监听输入框变化,当输入框内容改变时判断是否为空。
```
EditText editText = findViewById(R.id.edit_text);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().equals("")) {
// 输入框为空
} else {
// 输入框不为空
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)