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

Android - Make whole search view clickable with query hint

所以我一直在 Android Studio 中研究这个库存管理系统。在 Product Taking 的片段中,我有一个搜索视图,我想让这个搜索视图整个 body 都可以点击。此处解决了部分问题:. But i want the search view to have a visible query hint. So basically i want it to be a button with search icon and a text. I want that because it is supposed to open a dialog where the user actually going to search for products. Not from this search view.

When i add the setIconified(true) whole body is clickable but query hint is not visible. Like this:

当我添加 setIconified(false) 时,查询提示是可见的,但只有搜索图标是可点击的。像这样:

您可以在 SearchView 消耗它们之前拦截所有触摸。我创建了一个简单的 class 来拦截所有触摸事件。 科特林:

class TouchInterceptorLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    // You need override this method.
    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }
}

Java:

public class TouchInterceptorLayout extends FrameLayout {
    public TouchInterceptorLayout(Context context) {
        super(context);
    }

    public TouchInterceptorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchInterceptorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

看看 xml 的样子:

<com.example.testci.temp.TouchInterceptorLayout
            android:id="@+id/interceptorLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <SearchView
                android:id="@+id/searchView"
                android:queryHint="@string/app_name"
                android:iconifiedByDefault="false"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
</com.example.testci.temp.TouchInterceptorLayout>

现在您只需将 OnClickListener 设置为 interceptorLayout

您可以找到我实验的完整代码 here