热门提示输入之AutoCompleteTextView

前言:

在我们百度时,经常会遇到在你输入一个内容后在你的输入框的下面会提示一些别的热门的东西,接下来我们来简单实现这种功能

正文:

直接上代码,和之前的东西都大同小异

下面是Java代码

 1 public class MainActivity extends AppCompatActivity {
 2     private AutoCompleteTextView auto;
 3     private ArrayAdapter<String>adapter;
 4     private String[]str={"a1","a2","aa1","aa2"};
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         //初始化AutoCompleteTextView
10         auto=(AutoCompleteTextView)findViewById(R.id.auto);
11         //初始化适配器
12         adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);
13         //设置适配器
14         auto.setAdapter(adapter);
15     }
16 }

xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context="com.example.aaaaa.testautocompletetextview.MainActivity">
 7     <!--completionHint提示内容-->
 8     <!--completionThreshold设置你输入一个内容即可提示-->
 9     <AutoCompleteTextView
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:id="@+id/auto"
13         android:completionHint="请输入"
14         android:completionThreshold="1"/>
15 </RelativeLayout>

 

posted @ 2020-02-19 17:24  东功  阅读(233)  评论(0编辑  收藏  举报