如果想点击SearchView进行某个操作,直接用setOnClickListener是不生效的,setOnTouchListener也没有作用。
从源码看:SearchView继承自LinearLayout,里面的部件都是child,所以通过如下代码即可满足需要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static void setSearchViewOnClickListener(View v, OnClickListener listener) { if (v instanceof ViewGroup) { ViewGroup group = (ViewGroup)v; int count = group.getChildCount(); for (int i = 0; i < count; i++) { View child = group.getChildAt(i); if (child instanceof LinearLayout || child instanceof RelativeLayout) { setSearchViewOnClickListener(child, listener); } if (child instanceof TextView) { TextView text = (TextView)child; text.setFocusable(false); } child.setOnClickListener(listener); } } } |
表示设置每个child的setOnClickListener为相应listener,调用或也直接使用ViewUtils.setSearchViewOnClickListener:
1 2 3 4 5 6 |
setOnClickListener(searchView, new OnClickListener() { @Override public void onClick(View v) { } }); |
chanku fung