I have a view with radios, inputs and a button and when I click it, I want to check that all inputs contain information. How can I iterate through the view's elements in the activity and check if every textview meets the aforementioned requirement ? Thanks.
我有一个带无线电,输入和按钮的视图,当我点击它时,我想检查所有输入是否包含信息。如何遍历活动中的视图元素并检查每个textview是否符合上述要求?谢谢。
3 个解决方案
#1
110
I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "layout"):
我已经在一些我目前没有的代码中做了类似的事情,但是从内存中它应该是这样的(假设父视图LinearLayout的id为“layout”):
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
boolean success = formIsValid(layout);
public boolean formIsValid(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof EditText) {
//validate your EditText here
} else if (v instanceof RadioButton) {
//validate RadioButton
} //etc. If it fails anywhere, just return false.
}
return true;
}
#2
14
To apply the method by kcoppock recursively, you can change it to this:
要通过kcoppock递归应用该方法,您可以将其更改为:
private void loopViews(ViewGroup view) {
for (int i = 0; i < view.getChildCount(); i++) {
View v = view.getChildAt(i);
if (v instanceof EditText) {
// Do something
} else if (v instanceof ViewGroup) {
this.loopViews((ViewGroup) v);
}
}
}
#3
0
Your onClickListener
supplies the View v
object; use View rV = v.getRootView()
to position yourself on the form. Then use rV.findViewWithTag( ... )
or rV.findViewByID(R.id. ... )
to locate your form elements.
onClickListener提供View v对象;使用View rV = v.getRootView()将自己定位在表单上。然后使用rV.findViewWithTag(...)或rV.findViewByID(R.id ....)来定位表单元素。
#1
110
I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "layout"):
我已经在一些我目前没有的代码中做了类似的事情,但是从内存中它应该是这样的(假设父视图LinearLayout的id为“layout”):
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
boolean success = formIsValid(layout);
public boolean formIsValid(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof EditText) {
//validate your EditText here
} else if (v instanceof RadioButton) {
//validate RadioButton
} //etc. If it fails anywhere, just return false.
}
return true;
}
#2
14
To apply the method by kcoppock recursively, you can change it to this:
要通过kcoppock递归应用该方法,您可以将其更改为:
private void loopViews(ViewGroup view) {
for (int i = 0; i < view.getChildCount(); i++) {
View v = view.getChildAt(i);
if (v instanceof EditText) {
// Do something
} else if (v instanceof ViewGroup) {
this.loopViews((ViewGroup) v);
}
}
}
#3
0
Your onClickListener
supplies the View v
object; use View rV = v.getRootView()
to position yourself on the form. Then use rV.findViewWithTag( ... )
or rV.findViewByID(R.id. ... )
to locate your form elements.
onClickListener提供View v对象;使用View rV = v.getRootView()将自己定位在表单上。然后使用rV.findViewWithTag(...)或rV.findViewByID(R.id ....)来定位表单元素。