具有搜索ime选项的AutoCompleteTextView

时间:2019-03-02 05:46:26

标签: android search kotlin autocompletetextview ime

我想用autocompletetextview进行2种搜索。第一个是使用ArrayAdapter的基本版本(这个很好用),第二个是使用IME_OPTION_SEARCH或actionSearch。我想在这里讨论第二个问题。

基本上,我需要使用“ actionSearch”将imeOptions添加到autocompletetextview中,就像这样:

<AutoCompleteTextView
            android:layout_width="0dp"
            android:layout_height="wrap_content" android:id="@+id/acPetShopSearch"
            app:layout_constraintStart_toEndOf="@+id/ivPetShopSearchIcon" android:layout_marginStart="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" android:backgroundTint="@android:color/transparent"
            android:hint="@string/pet_shop_search_hint"
            android:imeOptions="actionSearch"
            android:inputType="textAutoComplete|textAutoCorrect"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
            android:textColorHint="@color/pinkish_grey" android:textColor="@color/purple_brown"
            android:textSize="12sp" android:singleLine="true"/>

然后,我将编辑器侦听器设置为带有actionSearch的简单EditText视图,如下所示:

acPetShopSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch(acPetShopSearch.text.toString())
                return true
            }
            return false
        }
    })

我尝试了这个,但出现了这样的错误:

  

java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数事件           在id.paw.pawcolony.view.petshop.PetShopActivity $ successCity $ 1.onEditorAction(未知来源:7)           在android.widget.TextView.onEditorAction(TextView.java:6271)           在com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)           在com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)           在com.android.internal.view.IInputConnectionWrapper $ MyHandler.handleMessage(IInputConnectionWrapper.java:85)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:201)           在android.app.ActivityThread.main(ActivityThread.java:6806)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

AutoCompleteTextView的行为是否不同于EditText?有什么方法可以将搜索功能放入AutoCompleteTextView?

1 个答案:

答案 0 :(得分:0)

acPetShopSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch(acPetShopSearch.text.toString())
                return true
            }
            return false
        }
    })

通过使KeyEvent可为空固定了我的