代码之家  ›  专栏  ›  技术社区  ›  Searene

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

  •  0
  • Searene  · 技术社区  · 7 年前

    WordAutoCompleteTextView ,扩展到 AutoCompleteTextView ,我希望组件完成以下两件事。

    1. 显示 type WordAutoCompleteTextView
    2. 显示 click 当用户单击 WordAutoCompleteTextView

    我只完成了第一个。对于第二个,当用户单击 , 类型 显示的是,而不是 点击 .换句话说, 类型 无论在何种情况下,都始终显示在下拉列表中。为什么?如何修复它?

    主要活动。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

    0 回复  |  直到 7 年前
        1
  •  0
  •   Yevhenii    7 年前

    把这个换成 :

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

    项目自动完成\u项目。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>