Android AoutCompleteTextView控件的使用

AoutCompleteTextView

功能:1>动态匹配输入的内容,如百度搜索引擎当输入文本时可以根据内容显示匹配的热门信息

2>独特属性:android:completionThreshold="2"<!--设置输入多少字符时自动匹配 -->

自动匹配的layout布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

<AutoCompleteTextView 
android:id="@+id/autocompletetextview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入需要查询的关键字"

android:completionThreshold="2"  />

自动匹配的class文件

package cn.androidstudy.demo;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

private AutoCompleteTextView acTextView;

private String[] res = { "shanghai", "beijing", "tianjing", "sichuan", "yibin", "sichuanjianzhuzhiyijishuxueyuan" };

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/** *  * 1.初始化控件:acTextView=(AutoCompleteTextView) * findViewByld(R.id.autocompletetextview) 

* 2.需要一个适配器:ArrayAdapter * <String> adapeter = new ArrayAdapter<String>(this, * android.R.layout.simple_expandable_list_item_1, res);

* 3.初始化数据源---这数据源自动去匹配文本框的内容private String[] res = { "shanghai", "beijing", * "tianjing", "sichuan", "yibin", "sichuanjianzhuzhiyijishuxueyuan" };

* 4.将adapeter和AoutCompleteTextView绑定:acTextView.setAdapter(adapeter); *  */

acTextView = (AutoCompleteTextView) findViewById(R.id.autocompletetextview);

ArrayAdapter<String> adapeter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, res);

acTextView.setAdapter(adapeter); }

@Override public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu); return true;

} }

posted @ 2015-09-14 23:36  星晨-XC  阅读(326)  评论(0编辑  收藏  举报