快速导航

Android之文本提示AutoCompleteTextView

Android 2017/03/15 01:03
属性 方法 说明
android:completionHint setCompletionHint() 设置下拉菜单中的提示标题
android:completionThreshold setThreshold() 设置至少输入几个字符才会提示
android:dropDownAnchor setDropDownHeight() 设置下拉菜单的高度
android:popupBackground setDropDownBackgroundResource() 设置背景

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.cheng.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请写出你喜欢的诗人:"
        android:textSize="21sp"/>
    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionHint="--请选择--"
        android:completionThreshold="1"/>

</LinearLayout>

MainActivity.java

package com.example.cheng.myapplication;

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

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView actv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        actv=(AutoCompleteTextView)findViewById(R.id.auto);
        String person[]=new String[]{"李白","苏辙","杜甫","苏轼","李清照","李贺","李商隐","苏洵"};
        //创建ArrayAdapter对
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,person);
        //为Spinner设置Adapter
        actv.setAdapter(aa);
    }
}

效果

AutoCompleteTextView只能完成一次提示,而如果我们想为用户提供多次填写的提示时,就可以使用AutoCompleteTextView的子类:MultiAutoCompleteTextView,MultiAutoCompleteTextView允许输入多个提示项,它会自动添加分隔符来分割各项。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.cheng.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请写出你喜欢的诗人:"
        android:textSize="21sp"/>

    <MultiAutoCompleteTextView
        android:id="@+id/multi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"/>

</LinearLayout>

MainActivity.java

package com.example.cheng.myapplication;

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

public class MainActivity extends AppCompatActivity {

    MultiAutoCompleteTextView mactv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mactv=(MultiAutoCompleteTextView) findViewById(R.id.multi);
        String person[]=new String[]{"李白","苏辙","杜甫","苏轼","李清照","李贺","李商隐","苏洵"};
        //创建ArrayAdapter对
        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,person);
        //为Spinner设置Adapter
        mactv.setAdapter(aa);
        //为MultiAutoCompleteTextView设置分隔符
        mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}

效果

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

Powered by codetiler.com