首页 > 代码库 > Android基础TOP5_1:AutoCompleteTextView自动补全文本框

Android基础TOP5_1:AutoCompleteTextView自动补全文本框

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:orientation="vertical"
 5     android:layout_height="match_parent"
 6     tools:context="com.example.top5_1.MainActivity" >
 7 
 8    <EditText
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:paddingLeft="5dp"
12         android:background="#FFA500"
13         android:textSize="16sp"
14         android:drawableLeft="@drawable/ic_launcher"
15         android:text="单词搜索补全演示" />
16    <AutoCompleteTextView
17       android:id="@+id/ed"
18       android:layout_width="fill_parent"
19       android:layout_height="wrap_content"
20       style="@android:attr/textViewStyle"
21       android:hint="请输入单词"
//设置从第二个字符开始提示 22 android:completionThreshold="2" 23 android:layout_marginTop="10dp" 24 25 /></LinearLayout>

JAVA:

 1 public class MainActivity extends Activity{
 2 private AutoCompleteTextView act;
 3 //设置数据源
 4 private static final String[] words={"abc","acd","ade"};
 5 
 6 protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         act=(AutoCompleteTextView) findViewById(R.id.ed);
10         //将数据源赋到文本框
11     ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
12             android.R.layout.simple_dropdown_item_1line,words);
13     act.setAdapter(aa);
14     };

技术分享

 

Android基础TOP5_1:AutoCompleteTextView自动补全文本框