AutoCompleteTextView 能够实现当用户输入信息的时候自动提示的功能,但是AutoCompleteTextView只能提示一次,而MultiAutoCompleteTextView也能实现信息提示的功能,而且是以逗号隔开,实现多次提示。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.autocompletetextviewdemo.MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="AutoCompleteTextView" /> <AutoCompleteTextView android:id="@+id/autotext" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="MultiAutoCompleteTextView" /> <MultiAutoCompleteTextView android:id="@+id/mul" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java:
import android.R.anim; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; public class MainActivity extends Activity { private AutoCompleteTextView autoCompleteTextView; private MultiAutoCompleteTextView multiAutoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autotext); String auto[] = new String[] { "计算机学院", "计算机科学与技术学院", "Google", "GoogleMap" }; // 第二个参数表示适配器的下拉风格 ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_dropdown_item_1line, auto); autoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.mul); multiAutoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());// 完成对选项的拆分功能,以逗号进行拆分 } }
时间: 2024-10-16 07:38:24