最终的效果如下:
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionHint="请选择你喜欢的歌曲" android:completionThreshold="1" android:dropDownHorizontalOffset="20dp" android:ems="10" android:text="AutoCompleteTextView" > <requestFocus /> </AutoCompleteTextView> </LinearLayout>
java代码为:
package android.demo; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.ImageView; public class AndroidDemo5Activity extends Activity { // 定义字符串数组作为提示的文本 String[] books = new String[] { "rollen", "rollenholt", "rollenren", "roll" }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); // 创建一个ArrayAdapter封装数组 ArrayAdapter<String> av = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, books); AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); auto.setAdapter(av); } }