|
MultiAutoCompleteTextView 与数组
①新建工程
②修改 main.xml 布局,添加一个 TextView、一个 MultiAutoCompleteTextView、一个 Button
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/TextView_InputShow"
android:layout_width="228px"
android:layout_height="47px"
android:text="请输入"
android:textSize="25px"
android:layout_x="42px"
android:layout_y="37px"/>
<MultiAutoCompleteTextView
android:layout_width="275px"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/MultiAutoCompleteTextView"
android:layout_x="23px"
android:layout_y="98px"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="127dip"
android:text="清空"
android:id="@+id/Button_clean"
android:layout_y="180dip"/>
</AbsoluteLayout>
复制代码
③MultiAutoCompleteTextView 是可以自动完成用户输入,能够添加多个输入,注意设置 Tokenizer
/*设置Tokenizer来确定用户输入文本的相关范围*/
myAutoCompleteTextView
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
复制代码
④修改 mainActivity.java,实现动态自动完成用户输入相关内容
package zyf.Ex_Ctrl_13_B;
/*导入使用的包*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.MultiAutoCompleteTextView;
public class Ex_Ctrl_13_B extends Activity {
/** Called when the activity is first created. */
/*定义要使用的类对象*/
private String[] normalString =
new String[] {
"Android", "Android Blog","Android Market", "Android SDK",
"Android AVD","BlackBerry","BlackBerry JDE", "Symbian",
"Symbian Carbide", "Java 2ME","Java FX", "Java 2EE",
"Java 2SE", "Mobile", "Motorola", "Nokia", "Sun",
"Nokia Symbian", "Nokia forum", "WindowsMobile", "Broncho",
"Windows XP", "Google", "Google Android ", "Google 浏览器",
"IBM", "MicroSoft", "Java", "C++", "C", "C#", "J#", "VB" };
private Button clean;
private MultiAutoCompleteTextView myAutoCompleteTextView;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 装入主屏布局main.xml */
setContentView(R.layout.main);
/* 以findViewById()从XML中获取UI元素对象 */
myAutoCompleteTextView =
(MultiAutoCompleteTextView) findViewById(R.id.MultiAutoCompleteTextView);
clean = (Button) findViewById(R.id.Button_clean);
/* new ArrayAdapter 对象并将normalString 字符串数组传入 */
/* 实现一个适配器对象,用来给自动完成输入框添加自动装入的内容 */
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, normalString);
/* 将ArrayAdapter 添加AutoCompleteTextView 对象中 */
/* 给自动完成输入框添加内容适配器 */
myAutoCompleteTextView.setAdapter(adapter);
/*设置Tokenizer来确定用户输入文本的相关范围*/
myAutoCompleteTextView
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
/* 给清空按钮添加点击事件处理监听器 */
clean.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 清空 */
myAutoCompleteTextView.setText("");
}
});
}
}
复制代码
结果:
|
|