Android 自动完成文本框的实例
时间:2021-07-25 08:39:24|栏目:Android代码|点击: 次
Android:自动完成文本框
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"> <AutoCompleteTextView android:id="@+id/myAutoCompleteTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
java文件代码如下:
package com.example.sample_5_1; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteActivity extends AppCompatActivity { private static final String[] myStr = new String[]{ "vae","victory","vision","virtue","vital" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto_complete); ArrayAdapter<String> aa = new ArrayAdapter<String>( //创建适配器 this, // Context android.R.layout.simple_dropdown_item_1line, //使用Android自带的简单布局 myStr); //资源数组 AutoCompleteTextView myAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView); //得到控件的引用 myAutoCompleteTextView.setAdapter(aa); //设置适配器 myAutoCompleteTextView.setThreshold(1); //定义需要用户输入的字符数 } }