android学习笔记五——AutoCompleteTextView

时间:2023-01-30 20:25:23

AutocompleteTextview

==>

使用比较容易,只需要为其设置一个Adapter,该Adapter封装其需要预设的文本内容。

如下所示实例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".IndexActivity" > <AutoCompleteTextView
android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="请选择你喜欢的书籍"
android:dropDownHorizontalOffset="20dp"
android:completionThreshold="1"
/> </RelativeLayout>

  

package com.example.myautocompletetextview;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; public class IndexActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.index); Log.i("swg", "autocompletetextview"); String[] books =
{ "孙悟空大战红孩儿", "小兵张嘎", "三毛流浪记", "鲁宾逊漂流记", "android高级编辑" }; ArrayAdapter<String> temp = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, books); AutoCompleteTextView txtView = (AutoCompleteTextView) this
.findViewById(R.id.autoText);
// 设置Adapter
txtView.setAdapter(temp);
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.index, menu);
return true;
} }