我在android中有5个EditTexts。我想知道我是否可以检查所有5个EditTexts是否为空。有什么办法可以做到吗?
当前回答
其他答案是正确的,但要用一种简短的方式
if(editText.getText().toString().isEmpty()) {
// editText is empty
} else {
// editText is not empty
}
其他回答
你也可以检查所有的EditText字符串在一个If条件:像这样
if (mString.matches("") || fString.matches("") || gender==null || docString.matches("") || dString.matches("")) {
Toast.makeText(WriteActivity.this,"Data Incomplete", Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(textA.getText())){
showToast(it's Null");
}
你可以使用TextUtils。isEmpty就像我的例子! 祝你好运
我曾经做过类似的事情;
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}
if ( (usernameEditText.getText()+"").equals("") ) {
// Really just another way
}
试试这个:
EditText txtUserName = (EditText) findViewById(R.id.txtUsername);
String strUserName = usernameEditText.getText().toString();
if (strUserName.trim().equals("")) {
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
或者像这样使用TextUtils类:
if(TextUtils.isEmpty(strUserName)) {
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件