Android實(shí)現(xiàn)實(shí)時(shí)搜索框功能
AutoCompleteTextView,自動(dòng)完成文本框。
用于實(shí)現(xiàn)允許用戶(hù)輸入一定字符后,顯示一個(gè)下拉菜單,供用戶(hù)從中選擇,當(dāng)用戶(hù)選擇某個(gè)選項(xiàng)后,按用戶(hù)選擇自動(dòng)填寫(xiě)該文本框。
該組件繼承EditText,所以它支持EditText組件提供的屬性,同時(shí),該組件該支持如下功能。

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="請(qǐng)輸入搜索內(nèi)容..." 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);
//創(chuàng)建一個(gè)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();
}
});
}
}
效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SearchView搜索框組件的使用方法
- Android搜索框通用版
- Android搜索框組件SearchView的基本使用方法
- Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中
- android搜索框上下滑動(dòng)變色效果
- Android搜索框SearchView屬性和用法詳解
- Android中如何實(shí)現(xiàn)清空搜索框的文字
- Android搜索框(SearchView)的功能和用法詳解
- Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android 改變圖標(biāo)原有顏色和搜索框的實(shí)例代碼
- Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
相關(guān)文章
Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
這篇文章主要介紹了Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法,涉及Android布局、窗口、事件監(jiān)聽(tīng)、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
這篇文章主要為大家詳細(xì)介紹了Android SwipeRefreshLayout仿抖音app靜態(tài)刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖片的切換顯示、透明度、大小調(diào)整等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android手機(jī)鬧鐘服務(wù)AlarmManagerk開(kāi)發(fā)案例
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)鬧鐘服務(wù)AlarmManagerk開(kāi)發(fā)案例的資料,需要的朋友可以參考下2016-05-05
Android6.0 Launcher2應(yīng)用解析
這篇文章主要為大家詳細(xì)介紹了Android6.0 Launcher2應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android使用RxJava實(shí)現(xiàn)預(yù)加載
這篇文章主要為大家詳細(xì)介紹了android使用RxJava實(shí)現(xiàn)預(yù)加載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android應(yīng)用開(kāi)發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析,深入的部分則是對(duì)左右手勢(shì)的識(shí)別給出了相關(guān)編寫(xiě)思路,需要的朋友可以參考下2016-02-02
Android開(kāi)發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
本文給大家分享android開(kāi)發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
mac開(kāi)發(fā)android環(huán)境搭建步驟圖解
這里比較詳細(xì)的來(lái)總結(jié)下mac開(kāi)發(fā)android的環(huán)境搭建步驟安裝過(guò)程,希望對(duì)一些正準(zhǔn)備配置Android開(kāi)發(fā)環(huán)境的小伙伴們有一定幫助2014-01-01

