Android - 使用查询提示使整个搜索视图可点击 [英] Android - Make whole search view clickable with query hint

查看:40
本文介绍了Android - 使用查询提示使整个搜索视图可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在 Android Studio 中开发这个库存管理系统.在 Product Take 的片段中,我有一个搜索视图,我想让这个搜索视图整个身体都可以点击.这个问题的一部分在这里解决:

So i've been working on this inventory management system in Android Studio. In the fragment for Product Taking i have a search view and i want to make this search views whole body to be clickable. Part of this problem is solved here:

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

When i add setIconified(false) query hint is visible but only search icon is clickable. Like this:

推荐答案

您可以在 SearchView 使用之前拦截所有触摸.我创建了一个简单的类来拦截所有触摸事件.科特林:

You can intercept all touches before SearchView consume them. I've created a simple class that intercept all touch events. Kotlin:

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 的样子:

See how xml looks:

<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.

Now you need just set OnClickListener to interceptorLayout.

您可以在此处找到带有我的实验的完整代码.

Full code with my experiment you can find here.

这篇关于Android - 使用查询提示使整个搜索视图可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆