最终的效果如下:
main.xml代码如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
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" >
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 av = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, books);
AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
auto.setAdapter(av);
}
}