Android - 通过查询提示使整个搜索视图可点击

所以我一直在Android Studio中研究这个库存管理系统。在产品获取的片段中,我有一个搜索视图,我想使这个搜索视图整个身体都是可点击的。这个问题的一部分在这里得到解决:Android - 使整个搜索栏可点击。但我希望搜索视图具有可见的查询提示。所以基本上我希望它是一个带有搜索图标和文本的按钮。我想要这样,因为它应该打开一个对话框,用户实际上将搜索产品。不是从此搜索视图中。

aluckdog
浏览 148回答 1
1回答

慕村225694

您可以在 SearchView 使用之前拦截所有触摸。我创建了一个拦截所有触摸事件的简单类。Kotlin:class TouchInterceptorLayout @JvmOverloads constructor(&nbsp; &nbsp; context: Context,&nbsp; &nbsp; attrs: AttributeSet? = null,&nbsp; &nbsp; defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {&nbsp; &nbsp; // You need override this method.&nbsp; &nbsp; override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }}爪哇岛:public class TouchInterceptorLayout extends FrameLayout {&nbsp; &nbsp; public TouchInterceptorLayout(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public TouchInterceptorLayout(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public TouchInterceptorLayout(Context context, AttributeSet attrs, int defStyleAttr) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyleAttr);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onInterceptTouchEvent(MotionEvent ev) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}看看 xml 的外观:<com.example.testci.temp.TouchInterceptorLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/interceptorLayout"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content">&nbsp; &nbsp; &nbsp; &nbsp; <SearchView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/searchView"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:queryHint="@string/app_name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:iconifiedByDefault="false"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"/></com.example.testci.temp.TouchInterceptorLayout>现在,您只需设置为 .OnClickListenerinterceptorLayout我的实验的完整代码可以在这里找到。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java