AutocompleteTextView点击监听器被调用两次

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

我有这个布局:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/input_birthdate"
    style="@style/ExposedDropDownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/birthdate">

    <AutoCompleteTextView
        android:id="@+id/autocomplete_birthdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false" />

</com.google.android.material.textfield.TextInputLayout>

使用此样式:

<!-- ExposedDropdownMenu -->
<style name="ExposedDropDownMenu" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
    <item name="boxStrokeColor">@color/text_input_layout_outlined_box_stroke</item>
    <item name="hintTextColor">@color/green_2</item>
</style>

我已经尝试在自动完成文本视图上设置点击侦听器:

autoComplete.setOnClickListener(view -> {
    // This is called twice when i click the autocomplete textview
});

听众被叫两次...为什么?我该如何解决?

编辑:删除样式,第一次单击(获得焦点)将被忽略,而第二次单击将被正确调用一次,但无论如何我都会失去“波纹”背景效果。

java android dropdown onclicklistener autocompletetextview
1个回答
0
投票

您可以尝试使用setOnTouchListener,它仅被调用一次,并且也保持了波纹。

autocomplete_birthdate.setOnTouchListener { view, motionEvent ->
    if (motionEvent.action == ACTION_UP) {
        // Some code
    }
    return false
}
© www.soinside.com 2019 - 2024. All rights reserved.