WEB开发网
开发学院手机开发Android 开发 Android之AutoCompleteTextView控件(自动提示功能... 阅读

Android之AutoCompleteTextView控件(自动提示功能)

 2010-05-08 18:27:00 来源:WEB开发网   
核心提示:res- >layout- >main.xml< ?xml version="1.0" encoding="utf-8"? >< LinearLayout xmlns:android="http://schemas.android.com/

res- >layout- >main.xml

< ?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"

>

< TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/ >

< AutoCompleteTextView

android:id="@+id/autotext"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:completionThreshold="1"

>< /AutoCompleteTextView >

< /LinearLayout >

android:completionThreshold="1" 至少输入几个字符,它才会具有自动提示的功能

src- >处理类

package com.pro;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

public class ProActivity extends Activity {

private final String[] data={

"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",

"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina"

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

AutoCompleteTextView m_autotext=(AutoCompleteTextView)findViewById(R.id.autotext);

// 数组适配器,它的作用就是一个数组和AutoCompleteTextView 之间的桥梁,它可以将数组里边定义的数据

//一一对应的显示在AutoCompleteTextView 里边。三个参数的基本意思是 1,上下文2,布局模板3,数据

ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, data);

m_autotext.setAdapter(adapter);

}

}

Tags:Android AutoCompleteTextView 控件

编辑录入:coldstar [复制链接] [打 印]
赞助商链接