1.简单介绍
AutoCompleteTextView有点像EditText和Spinner的组合。所以使用AutoCompleteTextView和使用Spinner差不多,用ArrayAdapter即可。如果对ArrayAdapter还不了解,可以参考Spinner的使用。
2.简单使用
- item_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/tv_week"
android:singleLine="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="#000000">
</TextView>
- activity_auto_complete_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".AutoCompleteTextViewActivity">
<AutoCompleteTextView
android:id="@+id/auto_tv_demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:completionHint="相关词语"
/>
</LinearLayout>
- AutoCompleteTextViewActivity.java
package xyz.strasae.androidlearn.my;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteTextViewActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextViewDemo;
private String[] mStrs = {"好", "不好", "好多了", "你好", "不知道", "不管我的事啊"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_text_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(AutoCompleteTextViewActivity.this, R.layout.item_dropdown, mStrs);
autoCompleteTextViewDemo = findViewById(R.id.auto_tv_demo);
autoCompleteTextViewDemo.setAdapter(adapter);
}
}
来源:https://blog.csdn.net/weixin_43219615/article/details/98966946