我已经为我的应用程序设置了一个搜索界面。如何根据作为字符串返回的搜索结果仅显示其文本包含搜索字符串的按钮?
问问题
29 次
1 回答
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;
}
于 2013-09-30T00:22:59.110 回答