|
自动完成输入框自动提示功能的菜单
AutoCompleteTextView 与数组
①新建工程
②修改 main.xml 布局,添加一个 TextView、一个 AutoCompleteTextView、一个 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"
>
</TextView>
<AutoCompleteTextView
android:id="@+id/AutoCompleteTextView_input"
android:layout_width="275px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:layout_x="23px"
android:layout_y="98px"
>
</AutoCompleteTextView>
<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="150dip">
</Button>
</AbsoluteLayout>
复制代码
③修改 mainActivity.java,添加自动完成功能
package zyf.Ex_Ctrl_13ME;
/*导入使用的包*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
public class Ex_Ctrl_13ME 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" };
@SuppressWarnings("unused")
private TextView show;
private AutoCompleteTextView autoTextView;
private Button clean;
private ArrayAdapter<String> arrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*装入主屏布局main.xml*/
setContentView(R.layout.main);
/*从XML中获取UI元素对象*/
show = (TextView) findViewById(R.id.TextView_InputShow);
autoTextView =
(AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView_input);
clean = (Button) findViewById(R.id.Button_clean);
/*实现一个适配器对象,用来给自动完成输入框添加自动装入的内容*/
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, normalString);
/*给自动完成输入框添加内容适配器*/
autoTextView.setAdapter(arrayAdapter);
/*给清空按钮添加点击事件处理监听器*/
clean.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*清空*/
autoTextView.setText("");
}
});
}
}
复制代码
结果:
|
|