Android-使用AutoCompleteTextView进行动态匹配

Android-使用AutoCompleteTextView进行动态匹配


在百度上面进行关键词搜索的时候,当我们输入时,就会显示一个下拉框,
匹配到我们输入的关键词,其实在安卓上面也有这个组件。
这里写图片描述


AutoCompleteTextView会实现自动匹配,如果不是复习以前的基础,还真不知道这个东西,嘿嘿,半桶水就是半桶水,还是得多多练习,查漏补缺啊。


这里写图片描述


小例子:

<LinearLayout 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:orientation="vertical"
              tools:context=".MainActivity"
    >

    <AutoCompleteTextView
        android:id="@+id/id_autotextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="3"
        android:hint="输入关键词"
        >


    </AutoCompleteTextView>


</LinearLayout>

android:completionThreshold=”3”
这一行代码表示的是,当我们输入次数到第三个的时候,就会出现匹配的下拉框。
MainActivity.java

package com.xieth.as.autocomtextviewdemo;

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

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView acTextView = null;

    private String[] res = {"beijing1", "beijing2", "beijing3", "shanghai1", "shanghai2", "guangzhou1", "shenzhen"};

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

        initViews();

        eventsViews();
    }

    private void eventsViews() {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res);

        acTextView.setAdapter(adapter);

    }

    private void initViews() {
        acTextView = (AutoCompleteTextView) findViewById(R.id.id_autotextView);
    }

}

效果:
这里写图片描述


还是很给力的,哈哈,不过数据源还是要自己去获取。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值