【问题标题】:AutocompleteTextView click listener called twiceAutocompleteTextView 点击监听器调用了两次
【发布时间】:2020-02-04 11:28:04
【问题描述】:

我有这个布局:

<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
});

监听器被调用了两次...为什么?我该如何解决这个问题?

编辑:删除样式,第一次点击(获得焦点)被忽略,第二次点击被正确调用一次,但我还是失去了“涟漪”背景效果。

【问题讨论】:

  • 试试 .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } });
  • 由于我无法发表评论,我将不得不在回答中提问:你确定添加事件监听器不会发生两次吗?
  • @AlessandroVerona 这正是我正在做的......
  • @Jumpa 你试过用断点调查这种行为吗?
  • @Jumpa ,覆盖 AutoCompleteTextView 内部的 onTouchEvent 并在函数内部返回 true 以消耗触摸事件

标签: java android dropdown onclicklistener autocompletetextview


【解决方案1】:

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

autocomplete_birthdate.setOnTouchListener { view, motionEvent ->
    if (motionEvent.action == ACTION_UP) {
        // Some code
    }
    return false
}

【讨论】:

    【解决方案2】:

    你应该试试下面的例子。

    <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Click Event"
                app:boxStrokeColor="@color/til_selector"
                app:boxStrokeErrorColor="@color/colorRed_700"
                app:boxStrokeWidth="1dp">
    
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etClickEvent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    **android:focusableInTouchMode="false"** />
    
            </com.google.android.material.textfield.TextInputLayout>
    

    现在以编程方式设置

    etClickEvent.inputType = 0
    etClickEvent.setOnClickListener{
    //PUT YOUR OWN 
    }
    

    这对我来说很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多