Android實現(xiàn)實時搜索框功能
更新時間:2017年09月29日 08:54:10 作者:PeterRabbit49
這篇文章主要為大家詳細介紹了Android實現(xiàn)實時搜索框功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
AutoCompleteTextView,自動完成文本框。
用于實現(xiàn)允許用戶輸入一定字符后,顯示一個下拉菜單,供用戶從中選擇,當用戶選擇某個選項后,按用戶選擇自動填寫該文本框。
該組件繼承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="請輸入搜索內(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)建一個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();
}
});
}
}
效果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android SearchView搜索框組件的使用方法
- Android搜索框通用版
- Android搜索框組件SearchView的基本使用方法
- Android EditText搜索框?qū)崿F(xiàn)圖標居中
- android搜索框上下滑動變色效果
- Android搜索框SearchView屬性和用法詳解
- Android中如何實現(xiàn)清空搜索框的文字
- Android搜索框(SearchView)的功能和用法詳解
- Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實例詳解
- Android自定義View實現(xiàn)搜索框(SearchView)功能
- Android 改變圖標原有顏色和搜索框的實例代碼
- Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
相關(guān)文章
View Controller Transition實現(xiàn)京東加購物車效果
這篇文章主要介紹了View Controller Transition實現(xiàn)京東加購物車效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02

