我有一个名为WordAutoCompleteTextView
的组件,它扩展了AutoCompleteTextView
,我希望该组件做以下两件事。
type
时,在下拉列表中显示WordAutoCompleteTextView
click
时在下拉列表中显示WordAutoCompleteTextView
我只取得了第一个。对于第二个,当用户单击AutoCompleteTextView
时,将显示type
而不是click
。换句话说,无论处于哪种状态,type
始终显示在下拉列表中。为什么?如何解决?
这是我的代码。
<?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>
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;
}
}
答案 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>