如何更改setOnClickListener()中AutoCompleteTextView下拉列表中显示的列表?

时间:2019-06-15 13:51:35

标签: java android

我有一个名为WordAutoCompleteTextView的组件,它扩展了AutoCompleteTextView,我希望该组件做以下两件事。

  1. 当用户输入type时,在下拉列表中显示WordAutoCompleteTextView
  2. 当用户单击click时在下拉列表中显示WordAutoCompleteTextView

我只取得了第一个。对于第二个,当用户单击AutoCompleteTextView时,将显示type而不是click。换句话说,无论处于哪种状态,type始终显示在下拉列表中。为什么?如何解决?

这是我的代码。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.dict.myapplication.WordAutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="7dp"
        android:ems="10"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <requestFocus />
    </com.dict.myapplication.WordAutoCompleteTextView>


</android.support.constraint.ConstraintLayout>

WordAutoCompleteTextView.java

package com.dict.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Objects;

public class WordAutoCompleteTextView extends android.support.v7.widget.AppCompatAutoCompleteTextView {
    public WordAutoCompleteTextView(Context context) {
        super(context);
        initView();
    }

    public WordAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WordAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        super.performFiltering("", 0);
    }

    private void initView() {
        ArrayAdapter<String> adapter = new ArrayAdapter<>
                (Objects.requireNonNull(getActivity(this)),
                        android.R.layout.select_dialog_item,
                        new ArrayList<>());
        setThreshold(0);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.clear();
                adapter.add("type");
                adapter.notifyDataSetChanged();
                showDropDown();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        setOnClickListener((view) -> {
            CharSequence text = ((TextView)view).getText();
            if (text.length() != 0) {
                adapter.clear();
                adapter.add("click");
                adapter.notifyDataSetChanged();
                showDropDown();
            }
        });
        setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
    }

    private Activity getActivity(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

}

enter image description here

1 个答案:

答案 0 :(得分:0)

WordAutoCompleteTextView.java 中进行更改:

  ArrayAdapter<String> adapter = new ArrayAdapter<>
                    (Objects.requireNonNull(getActivity(this)),
                            R.layout.autocomplete_item,
                            R.id.autoCompleteItem
                            new ArrayList<>());

并添加布局资源 item autocomplete_item.xml

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

    <TextView 
        android:id="@+id/autoCompleteItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="14sp"    
        />

</LinearLayout>