自動補全文本框AutoCompleteTextView

      此demo實現了自動補全文本框AutoCompleteTextView。


demo:下載地址


MainActivity.java:

package fk.androiddemo_018;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;
import java.util.List;

//0,這玩意至少需要輸入2個字符纔會提示
//1,佈局文件中聲明AutoCompleteTextView
//2,創建提示條目的佈局文件
//3,用ArrayAdapter給AutoCompleteTextView提供數據
//4,添加adapter
public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView autoText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> list=new ArrayList<>();
        list.add("bobo");
        list.add("lili");

        String ss[]=new String[]{"guagua","haha","gagaga","guoguo","huahua"};

//		ArrayAdapter adapter=new ArrayAdapter(
//				this,R.layout.list_item,R.id.text,list);
        //下面的添加方法比上面的更簡單
        ArrayAdapter adapter=new ArrayAdapter(
                this,R.layout.list_item,R.id.text,ss);

        autoText=(AutoCompleteTextView)findViewById(R.id.autoText);
        autoText.setAdapter(adapter);
    }
}

MainActivity佈局文件:

<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=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/autoText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" />

</RelativeLayout>

提示子條目佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<!--用於提示條目的佈局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:textSize="16sp"
        android:textColor="#000"/>

</LinearLayout>

運行截圖:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章