java - 如何在for循环中增加edittexts的数量
问题描述
我有 8个EditText
,我想检查这些EditText
s 是否为空。
这是我的代码:
if (edt1.getText().toString().equals("K")
&&!edt2.getText().toString().equals("")
&&!edt3.getText().toString().equals("")
&&!edt4.getText().toString().equals("")
&&!edt5.getText().toString().equals("")
&&!edt6.getText().toString().equals("")
&&!edt7.getText().toString().equals("")
&&!edt8.getText().toString().equals("")){
next();
}
这就是我要的:
int count=0;
for(int i=1;i<=8,i++){
if(!edt.getText().toString().equals(""))//how can I increase the number of edt, if i=1, edt1 and if i=2, edt2...
count++;
}
if(count==8){
next();
}
解决方案
在数组中逐行读取 EditText。那会简单得多。此外,您可以使用 String Utils http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html来检查空字符串,或者您可以使用默认的 null 和空字符串检查。
将 edt1,ed2,edt3 之类的引用放入一个数组并循环遍历它。那会奏效。
int count=0;
EditText edt[] = {edt1,edt2,edt3,edt4,edt5,edt6,edt7,edt8}
for(int i=0;i<=7,i++){
if(edt[0].getText().toString().equals("K") || !edt[i].getText().toString().equals(""))
count++;
}
if(count==8){
next();
}
推荐阅读
- python - 将箱线图和直方图组合成一个图
- 2d - KinematicBody2D 仅向左和向右移动
- javascript - AWS-Lambda 我收到一条错误消息“MissingRequiredParameter: Missing required key 'Data' in params”但没有调用“Data”参数
- mongodb - 如何在对多个字段 mongodb 聚合进行分组时获得单独的计数?
- nlp - 如何在pytorch中获得双向2层GRU的最终隐藏状态
- flutter - 从 Firebase 数据库中获取数据并匹配 Flutter 中的用户输入
- python - 动画蒙特卡洛方法 Pi 不同的颜色点
- javascript - 在画布中放置 clearRect()(带有文本动画)
- datatable - 数据表 - 将 td 包装到内容
- javascript - 使用 React 和 Express 从服务器解析 JSON - “TypeError: Cannot read property '' of undefined”