2013-09-29 45 views

回答

0

您可以循環通過的ViewGroup的孩子來搜索文本:

public static List<View> searchViews(ViewGroup group, String query) { 
    ArrayList<View> foundViews = new ArrayList<View>(); 
    query = query.toLowerCase(); 

    for (int i = 0; i < group.getChildCount(); i++) { 
    View view = group.getChildAt(i); 
    String text = null; 
    Class c = view.getClass(); 
    if (view instanceof Button) { // RadioButton is actually a subclass of Button 
     Button rb = (Button)view; 
     text = (String) rb.getText(); 
    } 
    // ... and maybe check other types of View 

    if (text == null) { 
     continue; 
    } 

    text = text.toLowerCase(); 
    if (text.contains(query)) { 
     foundViews.add(view); 
    } 
    } 

    return foundViews; 
} 
+0

感謝您的快速回答 – superuser