Android开发:AutoCompleteTextView的使用举例
Android开发:AutoCompleteTextView的使用举例
代码如下:
AppMain.java
- package lxy.litsoft;
- import java.util.ArrayList;
- import java.util.List;
- import Android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- public class AppMain extends Activity {
- AutoCompleteTextView autoCompleteTextView;
- List<String> list;
- ArrayAdapter<String> arrayAdapter;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //实例化AutoCompleteTextView对象
- autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete);
- //实例化List对象并添加item
- list = new ArrayList<String>();
- list.add("liu_zhen_wei");
- list.add("liu_zhen_wei_01");
- list.add("liu_zhen_wei_02");
- list.add("wang1");
- list.add("wang2");
- list.add("huohuo");
- //实例化ArrayAdapter对象为AutoCompleteTextView提供数据
- arrayAdapter = new ArrayAdapter<String>(this,R.layout.item,list);
- //为AutoCompleteTextView添加适配器
- autoCompleteTextView.setAdapter(arrayAdapter);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <AutoCompleteTextView
- android:id="@+id/autoComplete"
- android:layout_marginTop="100dip"
- android:layout_marginLeft="30dip"
- android:layout_marginRight="30dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" ></AutoCompleteTextView>
- </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"
- />
1.在布局文件中声明一个AutoCompleteTextView
2.在res/layout文件夹下新建一个布局文件,名为list_item.xml确定AutoComleteTextView的提示样式
3.创建一个ArrayAdapter为AutoCompleteTextView提供数据
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("abdd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,R.layout.item,list);
4.为AutoCompleteTextView添加适配器
autoCompleteTextView.setAdapter(array Adapter);
评论暂时关闭