自动完成文本框(AutoCompleteTextView)
java.lang.Object;
android.view.View;
android.view.TextView;
android.widget.EditText;
android.widget.AutoCompleteTextView;
MultiAutoCompleteTextView;
AutoCompleteTextView 类方法
AutoCompleteTextView 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9408072
下述程序中主要学习 AutoCompleteTextView 的使用方法,该控件无法在布局文件中指定 android:entries,暂时没有查证有没有其他指定的方法,因此决定采用代码加载!另一个需要注意 layout 中 <requestFocus /> 的使用。
1.MainActivity.java
-
package com.sweetlover.activity;
-
-
import com.sweetlover.autocompletetextviewdemo.R;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.ArrayAdapter;
-
import android.widget.AutoCompleteTextView;
-
-
public class MainActivity extends Activity {
-
-
private static final String[] phoneNumber = new String[]{
-
"111234", "133126", "303404", "796863"
-
};
-
-
private AutoCompleteTextView autoTextView = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
// TODO Auto-generated method stub
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
-
android.R.layout.simple_dropdown_item_1line, phoneNumber);
-
autoTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
-
autoTextView.setAdapter(adapter);
-
}
-
}
2.activity_main.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:padding="30dp"
-
android:orientation="vertical" >
-
-
<AutoCompleteTextView
-
android:id="@+id/autoCompleteTextView1"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center"
-
android:hint="@string/note"
-
android:ems="20" >
-
-
<requestFocus />
-
<!-- http://zhangkun716717-126-com.iteye.com/blog/802201 -->
-
</AutoCompleteTextView>
-
-
</LinearLayout>
3.string.xml
-
<resources>
-
-
<string name="app_name">AutoCompleteTextViewDemo</string>
-
<string name="note">输入电话号码</string>
-
-
</resources>
4.AndroidManifest.xml
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.sweetlover.autocompletetextviewdemo"
-
android:versionCode="1"
-
android:versionName="1.0" >
-
-
<uses-sdk
-
android:minSdkVersion="8"
-
android:targetSdkVersion="19" />
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity android:name="com.sweetlover.activity.MainActivity">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN"/>
-
<category android:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
-
</application>
-
-
</manifest>