AutoCompleteTextView ArrayAdapter

AutoCompleteTextView  

  继承于EditText,拥有EditText所有属性和方法

  在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息

 

首先在activity_main.xml中设置

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <AutoCompleteTextView
 7         android:id="@+id/actv"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:completionThreshold="2"
11         android:popupBackground="@android:color/holo_blue_light"
12         android:hint="请输入手机" />
13 
14 </RelativeLayout>

然后是AutoCompleteTextView提示框中显示的布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="horizontal" >
 5 
 6     <ImageView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:src="@drawable/ic_launcher" />
10 
11     <TextView
12         android:id="@+id/tv"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content" />
15 
16 </LinearLayout>

 

然后在MainActivity中将AutoCompleteTextView找到,初始化数据源,设置适配器

 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 import android.widget.ArrayAdapter;
 4 import android.widget.AutoCompleteTextView;
 5 
 6 public class MainActivity extends Activity {
 7 
 8     // 数据源
 9     String[] array = { "zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi",
10             "zhangba", "zhaojiu" };
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         // 显示布局
17         AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
18 
19         // 适配器
20         ArrayAdapter<String> adapter = new ArrayAdapter<String>(
21                 MainActivity.this, R.layout.item_layout, R.id.tv, array);
22 
23         //设置适配器
24         actv.setAdapter(adapter);
25     }
26 
27 }

 

运行效果如图

 

posted on 2016-10-12 10:54  语风6649  阅读(346)  评论(0编辑  收藏  举报

导航