gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:25:09 25 4
gpt4 key购买 nike

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

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

我只实现了第一个。对于第二个,当用户单击 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

最佳答案

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>

关于java - 如何更改 setOnClickListener() 中 AutoCompleteTextView 下拉列表中显示的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com