[android]代码库
1.MainActivity.java
package com.example.acer.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
// 定义字符串数组作为提示的文本
String[] books = new String[] { "rollen", "rollenholt", "rollenren", "roll" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建一个ArrayAdapter封装数组,simple_dropdown...是默认样式
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);
}
}
2.activity_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:hint="输入" >
</AutoCompleteTextView>
</LinearLayout>