开发者

Android Development: How To Get All EditText Children of a ViewGroup?

开发者 https://www.devze.com 2023-02-24 21:40 出处:网络
Basically, I have a LinearLayout that holds a random amount of horizontal LinearLayouts, and in each of the horizontal LinearLayouts there\'s a TextV开发者_如何学运维iew and an EditText. I want to be

Basically, I have a LinearLayout that holds a random amount of horizontal LinearLayouts, and in each of the horizontal LinearLayouts there's a TextV开发者_如何学运维iew and an EditText. I want to be able to get the value of each EditText children of the master LinearLayout.

Sorry if it's confusing, I'm no good at explaining things!

Could I just set the same ID for each of the EditTexts then use findViewById, or would that only return the first instance of an EditText?

Thanks, Alex.


 LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                     //in which you added your spinners at runtime ;

    int count = ll.getChildCount();
    for(int i =0;i<count;i++)
    {
        View v = ll.getChildAt(i);
        if(v instanceof Spinner)
        {
            // you got the spinner
            EditText s = (EditText) v;
            Log.i("Item selected",s.getText().toString());
        }
    }


findViewById returns only the first view with the given id. You're going to have to traverse the view hierarchy yourself, at least until you get down to each horizontal linear layout. You'll find the methods ViewGroup.getChildCount() and ViewGroup.getChildAt(int) useful for this.


You would need to call findViewById on each of the LinearLayouts. If you do this, you can set the same ID for each EditText.

0

精彩评论

暂无评论...
验证码 换一张
取 消