三星键盘退格键不适用于 SearchView

问题描述 投票:0回答:1

在我的 Android 应用程序中使用 SearchView 时,只有三星键盘的三星设备用户无法使用退格键。 三星键盘中的退格键可与 EditText 和除 SearchView 之外的任何其他输入视图一起正常工作。 我试图检测用户何时按下退格键代码并手动删除一个字母,但也不起作用。 是三星键盘的问题吗?我该如何克服这个问题?

java android searchview samsung-mobile
1个回答
0
投票

我知道这是旧帖子,但我遇到了同样的问题,所以我会发布我的解决方案。该问题是由

SearchView
使用覆盖
EditText
的扩展
onKeyPreIme
引起的,并且不传递
KeyEvent.KEYCODE_BACK
事件。
我使用的解决方案并不完全安全,因为我必须覆盖 RestrictedAPI,但这应该不是问题。
该解决方案使用 androidx 库版本,但可以针对较旧的 appcompat 库进行编辑。

首先我们创建一个类

MySearchViewSearchAutoComplete
覆盖内部静态
androidx.appcompat.widget.SearchView.SearchAutoComplete
类。这个类覆盖
onKeyPreIme
,并在需要时拦截
KeyEvent.KEYCODE_BACK
事件,将它们交换为
KeyEvent.KEYCODE_DEL
事件,并将它们直接发送到EditBox。

这是代码;

package com.example.widget.search;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;

@SuppressLint("RestrictedApi")
public class MySearchViewSearchAutoComplete extends androidx.appcompat.widget.SearchView.SearchAutoComplete
{
    boolean _isNonTouch;
    boolean _isIgnore;

    public MySearchViewSearchAutoComplete(Context context)
    {
        super(context);
        init(context);
    }

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

    public MySearchViewSearchAutoComplete(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context)
    {
        _isNonTouch = context.getResources().getConfiguration().touchscreen == android.content.res.Configuration.TOUCHSCREEN_NOTOUCH;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event)
    {
        if (_isNonTouch && keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN) // on ActionUp it may already be empty, but we should treat it as not empty
            {
                _isIgnore = getSelectionStart() == 0 && getSelectionEnd() == 0;
            }
            if (!_isIgnore)
            {
                event = new KeyEvent(event.getDownTime(), event.getEventTime(), event.getAction(), KeyEvent.KEYCODE_DEL, event.getRepeatCount(), event.getMetaState(), event.getDeviceId(), event.getScanCode(), event.getFlags(), event.getSource());
                dispatchKeyEvent(event);
                return true;
            }
        }
        return super.onKeyPreIme(keyCode, event);
    }
}

然后我们覆盖

abc_search_view
布局以使用上面的类。我们使用原始源代码,只是将
EditText
类更改为我们上面的扩展类。创建一个名为
abc_search_view.xml
的布局文件这是代码;

<?xml version="1.0" encoding="utf-8"?><!--
    /*
     * Copyright (C) 2014 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <!-- This is actually used for the badge icon *or* the badge label (or neither) -->
    <TextView
        android:id="@+id/search_badge"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dip"
        android:drawablePadding="0dip"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="?android:attr/textColorPrimary"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/search_button"
        style="?attr/actionButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/abc_searchview_description_search"
        android:focusable="true" />

    <LinearLayout
        android:id="@+id/search_edit_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_weight="1"
        android:layoutDirection="locale"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/search_mag_icon"
            style="@style/RtlOverlay.Widget.AppCompat.SearchView.MagIcon"
            android:layout_width="@dimen/abc_dropdownitem_icon_width"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:scaleType="centerInside"
            android:visibility="gone" />

        <!-- Inner layout contains the app icon, button(s) and EditText -->
        <LinearLayout
            android:id="@+id/search_plate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:orientation="horizontal">

            <view
                android:id="@+id/search_src_text"
                class="com.example.widget.search.MySearchViewSearchAutoComplete"
                android:layout_width="0dp"
                android:layout_height="36dip"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:background="@null"
                android:dropDownAnchor="@id/search_edit_frame"
                android:dropDownHeight="wrap_content"
                android:dropDownHorizontalOffset="0dip"
                android:dropDownVerticalOffset="0dip"
                android:ellipsize="end"
                android:imeOptions="actionSearch"
                android:inputType="text|textAutoComplete|textNoSuggestions"
                android:paddingLeft="@dimen/abc_dropdownitem_text_padding_left"
                android:paddingRight="@dimen/abc_dropdownitem_text_padding_right"
                android:singleLine="true" />

            <ImageView
                android:id="@+id/search_close_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/abc_searchview_description_clear"
                android:focusable="true"
                android:paddingLeft="8dip"
                android:paddingRight="8dip" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/submit_area"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/search_go_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/abc_searchview_description_submit"
                android:focusable="true"
                android:paddingLeft="16dip"
                android:paddingRight="16dip"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/search_voice_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/abc_searchview_description_voice"
                android:focusable="true"
                android:paddingLeft="16dip"
                android:paddingRight="16dip"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

就是这样,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.