您的位置:首页 > 移动开发 > Android开发

android 组件AutoCompleteTextView和MultiAutoCompleteTextView实例

2015-08-21 18:27 453 查看
AutoCompleteTextView--自动补全编辑框。

MultiAutoCompleteTextView多项自动补全编辑框。(可以根据特定的分隔符,如逗号“,”来多项提示内容。)

根据输入的前若干字母编辑框会自动下拉展示待选的字符串,类似百度搜索的历史记录栏。

android:completionThreshold="1"设置这个属性可以定义从输入的第几个字母开始提示。此处是1,说明当输入第一个字母时就会有提示。如果不设置此项,则默认的是2

布局文件:autocomplete_textview_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="6dp" >

<AutoCompleteTextView

android:id="@+id/autoCompleteTextView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:completionThreshold="1"

android:hint="请输入字符串如:hello"

android:ems="10"

android:textColor="#ff000000" >

<requestFocus />

</AutoCompleteTextView>

<MultiAutoCompleteTextView

android:id="@+id/multiAutoCompleteTextView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入字符串如:hello"

android:ems="10"

android:textColor="#ff000000" />

</LinearLayout>

AtyAutoCompleteTextView.java文件:

package com.fxj.composit;

import com.fxj.compractice.R;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.MultiAutoCompleteTextView;

public class AtyAutoCompleteTextView extends Activity {

private AutoCompleteTextView textView; //自动补全编辑框

private MultiAutoCompleteTextView mulTextView;//多项自动补全编辑框


private ArrayAdapter<String> adapter; //自动补全编辑框的适配器

private ArrayAdapter<String> multAdapter; //多项自动补全编辑框的适配器

private String [] strs= new String[]{"abc", //数据字符串数组

"abcd",

"abcde",

"java",

"javascript",

"hello",

"hello world",

"hello 你好"

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.autocomplete_textview_layout);

//实例化控件

textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

mulTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);

//实例化适配器

adapter = new ArrayAdapter<String>(this, R.layout.item_autocomplete_textview,strs);

multAdapter = new ArrayAdapter<String>(this, R.layout.item_autocomplete_textview, strs);

//为多项自动补全编辑框设置分隔符,这里用的是默认的“,”分隔符。

mulTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());


//设置适配器

mulTextView.setAdapter(multAdapter);

textView.setAdapter(adapter);

}

}

运行效果:





结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: