当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现实时搜索框功能

Android实现实时搜索框功能

2019年07月24日  | 移动技术网移动技术  | 我要评论
autocompletetextview,自动完成文本框。 用于实现允许用户输入一定字符后,显示一个下拉菜单,供用户从中选择,当用户选择某个选项后,按用户选择自动填写

autocompletetextview,自动完成文本框。

用于实现允许用户输入一定字符后,显示一个下拉菜单,供用户从中选择,当用户选择某个选项后,按用户选择自动填写该文本框。

该组件继承edittext,所以它支持edittext组件提供的属性,同时,该组件该支持如下功能。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.amy.searchtest.mainactivity"> 
<linearlayout 
 android:orientation="horizontal" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 
 <autocompletetextview 
 android:id="@+id/autocompletetextview1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="" 
 android:completionthreshold="2" 
 android:completionhint="请输入搜索内容..." 
 android:layout_weight="7"/> 
 
 <button 
 android:id="@+id/button1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="搜索" 
 android:layout_weight="1" 
 android:layout_marginleft="10px"/> 
 
 
</linearlayout> 
 
</android.support.constraint.constraintlayout> 

mainactivity.java

package com.amy.searchtest; 
 
import android.support.v7.app.appcompatactivity; 
import android.os.bundle; 
import android.view.view; 
import android.widget.arrayadapter; 
import android.widget.autocompletetextview; 
import android.widget.button; 
import android.widget.toast; 
 
public class mainactivity extends appcompatactivity { 
 
 public static final string[] contents = new string[]{"zg陕西","zg海南","zg新疆","zg西藏"}; 
 autocompletetextview textview; 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 textview = (autocompletetextview)findviewbyid(r.id.autocompletetextview1); 
 //创建一个arrayadapter适配器 
 arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_dropdown_item_1line,contents); 
 textview.setadapter(adapter); 
 
 button button = (button) findviewbyid(r.id.button1); 
 button.setonclicklistener(new view.onclicklistener() { 
  @override 
  public void onclick(view v) { 
  toast.maketext(mainactivity.this, textview.gettext().tostring(),toast.length_short).show(); 
  } 
 }); 
 } 
 
} 

效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网