Android Technology--find controls in the casual view of Android

Source: Internet
Author: User

1, in very many cases, we may not know the ID of the control, but we want to include this control in the view to find it, can be used for example, the following practices:

Example: Find all of the button controls in the activity's root view

private void Findbutton (ViewGroup group, list<button> result)
{
if (group! = null)
{
for (int i = 0, j = group.getchildcount (); i < J; i++)
{
View child = Group.getchildat (i);
if (Child instanceof Button)
{
Result.add (Button) child);
} else if (child instanceof ViewGroup)
{
Findbutton ((viewgroup) child, result);
}
}
}
}

Called in the activity:

list<button> result = new arraylist<button> ();
This.findbutton ((ViewGroup) This.getwindow (). Getdecorview (), result);

In fact, this approach is recursively starting from the root view to find the entire control tree, finally find the control that meets the requirements, a little rewrite will be able to meet the needs of a variety of search control.

Android Technology--find controls in the casual view of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.