android示例之AutoCompleteTextView

来源:互联网 发布:java http base64 编辑:程序博客网 时间:2024/04/28 23:15

AutoCompleteTextView是用来自动提示或者补全用户的输入的,好处是用户可以减少输入量。

效果图:


使用AutoCompleteTextView的步骤是:

1、设置AutoCompleteTextView的布局;

2、设置用户输入时,提示的内容列表的样式文件;

3、设置数据源和适配器;

4、将AutoCompleteTextView和ArrayAdapter联系起来;

代码:

AutoCompleteTextView的布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="测试"/>    <AutoCompleteTextView         android:id="@+id/auto"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/></LinearLayout>

提示内容的样式表的样式:

<?xml version="1.0" encoding="utf-8"?><TextView    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="10dp"    android:textSize="16sp"    android:textColor="#FF4500"/>

Activity中:

public class AutoCompleteTextViewActivity extends Activity {private AutoCompleteTextView auto;//显示提示的数据来源private String[] list={"afa","Afa","afb","afc","afd","afe","aff","afg","afh","afi","bf"};//显示的适配器private ArrayAdapter<String> arrayAdapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //得到自动输入框架        auto=(AutoCompleteTextView)findViewById(R.id.auto);        //得到带有数据源的适配器        arrayAdapter=new ArrayAdapter<String>(this,R.layout.list,list);        //把ArrayAdapter设置给AutoCompleteTextView,将自动输入框和适配器联系起来        auto.setAdapter(arrayAdapter);    }}

根据结果可以看出,提示列表的数据显示顺序和数据源的提供数据的顺序是一致的。

0 0
原创粉丝点击