当前位置: 代码迷 >> Android >> AutoCompleteTextView项不可单击/不可选择
  详细解决方案

AutoCompleteTextView项不可单击/不可选择

热度:57   发布时间:2023-08-04 12:23:53.0

假设我有以下AutoCompleteTextView

final AutoCompleteTextView actvCategory = (AutoCompleteTextView) findViewById(R.id.actvCategory);
actvCategory.setAdapter(new ArrayAdapter<String>(this, R.layout.category_list_item_layout, R.id.category_list_item_title, items));
actvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        actvCategory.setText(items.get(i));
    }
});

使用以下项目布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/category_list_item_title"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|left"
        android:background="@drawable/np_blue"
        android:focusable="false"
        android:layout_height="match_parent"/>

    <ImageButton
        android:id="@+id/category_list_item_delete_btn"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:src="@drawable/delete_item"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:background="@drawable/np_light_blue"
        android:focusable="false"
        android:layout_height="match_parent" />

</LinearLayout>

当用户开始在文本字段中键入内容时,会显示正确的建议,但是这些建议是不可点击的(=当用户单击它时,没有任何反应)。 据我所知,问题出在布局文件中的ImageButton上,当我删除它时,一切都按预期进行,但是当它存在时,就会出现此问题。 我也尝试过focusable=false ,但是没有运气。

谁能帮我解决问题所在?
谢谢

由于您的ImageButton是不可单击的,因此您可能要使用ImageView

它的工作原理与ImageButton大致相同,但不可点击。