您好!欢迎来到源码码网

Android自动完成文本框控件AutoCompleteTextView使用

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-10 16:10
  • 阅读:519

在使用百度或者 Google 搜索信息时,只需要在搜索框中输入几个关键字,就会有很多相关的信息以列表形式被列举出来供用户选择,这种效果在 Android SDK 中可以通过 AutoCompleteTextView 来实现。

下面用一个简单的实例讲解 AutoCompleteTextView 的使用方法。在工程 WidgetDemo 的布局文件 main.xml 中添加一个 Button,用以启动 AutoCompleteTextViewActivity。

在 main.xml 中添加代码如下:

  1. <Button

  2.     android:id="@+id/button5"

  3.     android:layout_width="wrap_content"

  4.     android:layout_height="wrap_content"

  5.     android:text="AutoCompleteTextViewDemo"/>

单击 Button 并启动 AutoCompleteTextViewActivity 的代码如下:


  1. Button autobtn = (Button)this.findViewById(R.id.button5);

  2. autobtn.setOnClickListener(new OnClickListener(){

  3. @Override

  4.   public void onClick(View v){

  5.        Intent intent = new Intent(WidgetDemoActivity.this, AutoCompleteTextViewActivity.class);

  6.        startActivity(intent);

  7.      }

  8. });


同时在 AndroidManifest.xml 文件中声明该 Activity:

<activity android:name=".AutoCompleteTextViewActivity"></activity>

AutoCompleteTextViewActivity 的运行效果如图 1 所示。

AutoCompleteTextViewActivity的运行效果

图 1  AutoCompleteTextViewActivity 的运行效果

AutoCompleteTextViewActivity 使用的布局文件为 autocompletetextview.xml,其具体内容如下:


  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical">


  6. <TextView

  7. android:layout_width="fill_parent"

  8. android:layout_height="wrap_content"

  9. android:text="AutoCompleteTextView演示:" />


  10. <AutoCompleteTextView

  11. android:id="@+id/autoCompleteTextView1"

  12. android:layout_width="match_parent"

  13. android:layout_height="wrap_content"

  14. android:text="" />


  15. <requestFocus />

  16. </LinearLayout>

AutoCompleteTextViewActivity.java 的代码如下:


package introduction.android.widgetdemo;

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

public class MainActivity extends Activity {
    private AutoCompleteTextView textView;
    private static final String[] autotext = new String[]{"张三","张无忌","张三丰"};
   
    @Override
    public void onCreat(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocompletetextview);
        textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        /*new ArrayAdapterd 对象将 autotext字符串数组传入*/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,android.R.layout.simple_dropdown_item_1line,autotext);
        /*将ArrayAdapter添加到AutoCompleteTextView中*/
        textView.setAdapter(adapter);
    }
}

AutoCompleteTextViewActivity 中为可自动补全的内容建立对应字符串数组 autotext,将该数组关联到 ArrayAdapter 中,然后将 ArrayAdapter 与 AutoCompleteTextView 相关联,进而实现自动完成文本功能。

AutoCompleteTextView 提供一系列属性对显示效果进行设置,分别说明如下。

  • completionThreshold:它的值决定了你在 AutoCompleteTextView 中至少输入几个字符,才会具有自动提示的功能。另外,默认最多提示 20 条。

  • dropDownAnchor:它的值是一个 View 的 ID,指定后,AutoCompleteTextView 会在这个 View 下弹出自动提示。

  • dropDownSelector:应该是设置自动提示的背景色之类的,没有尝试过,有待进一步考证。

  • dropDownWidth:设置自动提示列表的宽度。


特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • 常用测试压力工具使用介绍
  • 常用测试压力工具使用介绍
  • ab 是 ApacheBench 工具的缩写,它是一个HTTP压力测试工具。让我详细说明如何测试:1. 安装ApacheBenchWindows系统:方法一:安装XAMPP或WAMP(自带ab)下载地址:https://www.apachefriends.org/zh_cn/index.html安装后,ab工具在:C:xamppapacheinab.exe方法二:使
  • 开发工具
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-13 20:27
  • 阅读:56
  • 工程项目一体化自动管理软件解决方案
  • 工程项目一体化自动管理软件解决方案
  • 1.项目概述1.1项目背景在工程建设行业数字化转型浪潮下,传统项目管理面临信息孤岛、协同困难、进度不可控、成本超支等痛点。本方案旨在构建一个覆盖工程项目全生命周期、全参与方、全业务流程的一体化智能管理平台。1.2解决方案愿景打造数据驱动、智能协同、风险预警、自动执行的工程大脑,实现:管理流程自动化率≥80%项目协同效率提升40%成本偏差率降低至±3%以内安全事故发生率降低60%1.3目标用户矩阵┌───────────────┬
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:26
  • 阅读:169
  • 车辆管理系统需求文档与技术架构PC端+小程序
  • 车辆管理系统需求文档与技术架构PC端+小程序
  • 第一部分:需求文档1.项目概述1.1项目背景为企事业单位、车队运营商、租赁公司等提供一套完整的车辆全生命周期管理解决方案,实现车辆管理数字化、智能化。1.2项目目标建立车辆从购置到报废的全流程管理体系实现用车申请、调度、监控、结算的闭环管理通过数据分析优化车辆使用效率降低车辆运维成本20%以上1.3用户角色矩阵┌──────────────┬─────────────────────────────┬──────────────
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:11
  • 阅读:157
  • 智慧农业/渔业物联网系统需求文档
  • 智慧农业/渔业物联网系统需求文档
  • 智慧农业/渔业物联网系统需求文档文档版本: V1.0项目目标: 构建一个集环境智能监测、设备自动化控制、生长模型分析、溯源管理与远程指挥于一体的综合物联网管理平台,实现降本增效、提质增产、风险预警与品牌增值。1.系统总体概述1.1核心价值: 数据驱动决策,解放人力,实现农业/渔业生产的精准化、自动化与智能化。1.2用户角色:生产员/养殖员: 现场巡视、接收告警、执行设备手动控制、查看实时环境
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:04
  • 阅读:68
  • 程序员AI编程工具推荐
  • 程序员AI编程工具推荐
  • AI编程工具是当前开发者的“副驾驶”,能够极大提升开发效率。以下我将从通用型、代码专用型、垂直领域型以及开源/自部署型几个维度为您分类推荐,并附上它们的核心特点和适用场景,帮助您选择。一、通用型AI对话助手(编程是核心能力之一)这类工具本质是“更懂代码的ChatGPT”,适合处理广泛的编程问题、解释代码、生成文档等。ChatGPT(GPT-4/4o)简介:行业标杆,尤其在GPT-4版本下,代码理解和生成能力极强。优点:上下文能力强,
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 10:56
  • 阅读:96
联系客服
源码代售 源码咨询 技术开发 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部