1.XML文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <AutoCompleteTextView
- android:id="@+id/autoCompleteTextView1"
- android:text=""
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <requestFocus></requestFocus>
- </AutoCompleteTextView>
- </LinearLayout>
2.java文件
- package test.AutoCompleteTextView;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- public class HelloAutoCompleteTextViewActivity extends Activity {
- /** Called when the activity is first created. */
- //宣告一個AutoCompleteTextView物件為actv
- //實現ArrayAdapter產生str字串陣列的適配器
- //setAdapter()將適配器設定給actv
- private AutoCompleteTextView actv;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
- String[] str = {"abc" , "abcd" , "bcd" , "bcde"};
- ArrayAdapter adapter = new ArrayAdapter(this , android.R.layout.simple_dropdown_item_1line , str);
- actv.setAdapter(adapter);
- }
- }
沒有留言:
張貼留言