AutoCompleteTextView的使用
使用AutoCompleteTextView产生的效果类似于手机联系人的查询,输入手机号的前半部分就会补充完整后半部分。
1、在layout中添加AutoCompleteTextView控件,并给出id
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2、AutoCompleteTextView的使用也需要适配器,这里采用ArrayAdapter
package com.grid.administrator.mywidgeview;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
private AutoCompleteTextView mautoCompleteTextView;
private ArrayAdapter adapter;
private String[] mData=
{"layout_linear","layout_relative","layout_frame","layout_table
","view_text","view_img","edit"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mautoCompleteTextView= (AutoCompleteTextView)
findViewById(R.id.autocomplete);
adapter=new ArrayAdapter
(this,android.R.layout.simple_list_item_1,mData);
mautoCompleteTextView.setAdapter(adapter);
}
}