Android的SearchView实现点击跳转
SearchView是android原生的搜索框控件,在写项目的时候需要实现类似淘宝的搜索框点击效果(点击搜索框跳转到一个新的activity实现搜索),首页的搜索框有些实现是自己设计(用Textview和Button那些自己设计UI),本着偷懒的精神!直接用SearchView实现首页的跳转。
问题
SearchView的setOnClick不起作用,SearchView的所有监听事件我都试过,失去焦点撒的都试过(可能自己操作问题),始终无法实现点击SearchView的任意位置都可以跳转,只能点击那个SearchIcon才可以跳转,搞了半天,最终在网上找到一个土办法,先定义一个FrameLayout(控件始终处于左上角),然后里面加上SearchView和View(两个大小要一样),然后把SearchView的focusable设为false,最后给View来一个监听(点击跳转事件)就成了。
代码(XML)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<SearchView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:focusable="false"
android:queryHint="点击搜索"
android:iconifiedByDefault="false"/>
<View
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="40dp" />
</FrameLayout>