阅读《Android 从入门到精通》(12)——自己主动完毕文本框

自己主动完毕文本框(AutoCompleteTextView)

java.lang.Object;

android.view.View;

android.view.TextView;

android.widget.EditText;

android.widget.AutoCompleteTextView;

MultiAutoCompleteTextView;

AutoCompleteTextView 类方法

阅读《Android 从入门到精通》(12)——自己主动完毕文本框

AutoCompleteTextView 演示样例

完整project: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>
上一篇:springmvc 配置异步请求


下一篇:RoboGuice 3.0 (三)总结篇