如何遍历视图的元素遍历、视图、元素

2023-09-05 06:11:02 作者:感情流浪汉

我有一个收音机,输入和一个按钮的看法,当我点击它,我要检查所有输入包含的信息。我如何能遍历活动视图的元素,并检查每一个TextView的满足上述要求?谢谢你。

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.

推荐答案

我已经做了一些code相似,我不跟我此刻有,但是从内存中它应该是这样的(假设以布局的ID)父视图LinearLayout中:

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 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;
}