Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a
自動(dòng)提示文本框(AutoCompleteTextView)可以加強(qiáng)用戶體驗(yàn),縮短用戶的輸入時(shí)間(百度的搜索框就是這個(gè)效果)。
先給大家展示下效果圖,如果大家感覺(jué)還不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼:

最后一張獲取文本框里面的值(其實(shí)就跟TextView、EditText一樣):

首先,在xml中定義AutoCompleteTextView控件:
activity_main.xml:
<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:gravity="center" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/actv_game" android:layout_width="220dp" android:layout_height="wrap_content" android:completionHint="@string/game_" android:completionThreshold="1" android:hint="@string/game" /> <AutoCompleteTextView android:id="@+id/actv_car" android:layout_width="220dp" android:layout_height="wrap_content" android:completionHint="@string/car_" android:completionThreshold="1" android:hint="@string/car" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:onClick="getValue" android:text="@string/button" /> </LinearLayout>
屬性completionHint是提示數(shù)據(jù)時(shí)候顯示給用戶看的提示信息:
android:completionHint="@string/game_"
屬性completionThreshold是提示的起始位置,默認(rèn)值為2,即輸入兩個(gè)字符之后開(kāi)始檢索。一般設(shè)置為1:
android:completionThreshold="1"
這里有兩個(gè)AutoCompleteTextView,一個(gè)從xml中獲取提示數(shù)據(jù),另一個(gè)從集合中拿取提示數(shù)據(jù)。
Strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">actv_demo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="game">游戲</string> <string name="car">車</string> <string name="game_">請(qǐng)選擇你喜歡的游戲</string> <string name="car_">請(qǐng)選擇你喜歡的車</string> <string name="button">獲取文本框的值</string> <string-array name="games"> <item>魔獸</item> <item>魔獸1</item> <item>魔獸2</item> <item>仙劍</item> <item>仙劍1</item> <item>仙劍2</item> <item>CS</item> <item>CS1</item> <item>CS2</item> <item>CF</item> <item>CF1</item> <item>CF2</item> <item>DNF</item> <item>DNF1</item> <item>DNF2</item> <item>傳奇</item> <item>傳奇1</item> <item>傳奇2</item> <item>天下</item> <item>天下1</item> <item>天下2</item> </string-array> </resources>
在String.xml中定義好games數(shù)組 。
MainActivity.java:
package com.yx.actv_demo.ui;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import com.yx.actv_demo.R;
/**
*
* 此類描述的是: 主界面
*
* @author: CS YX
* @version:1.0
* @date:2014-10-24 下午3:47:38
*/
public class MainActivity extends Activity {
// 游戲文本框
private AutoCompleteTextView actv_game;
// 游戲文本框適配器
private ArrayAdapter<CharSequence> gameAdapter;
// 車
private AutoCompleteTextView actv_car;
private ArrayAdapter<String> carAdapter;
private List<String> cars;// 集合數(shù)據(jù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();// 初始化控件
initData();// 初始化數(shù)據(jù)
// 實(shí)例化適配器 (從xml中拿取數(shù)據(jù))
gameAdapter = ArrayAdapter.createFromResource(MainActivity.this,
R.array.games, android.R.layout.simple_spinner_item);
// 綁定適配器顯示數(shù)據(jù)
actv_game.setAdapter(gameAdapter);
// 實(shí)例化適配器 從數(shù)組或集合中拿去數(shù)據(jù)
carAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, cars);
// 綁定適配器顯示數(shù)據(jù)
actv_car.setAdapter(carAdapter);
}
/**
*
* 此方法描述的是: 獲取文本框的值
*
* @param view
* view對(duì)象
*/
public void getValue(View view) {
String game = "";
if (actv_game != null) {
game = actv_game.getText().toString();//獲取文本框的值
}
String car = "";
if (actv_car != null) {
car = actv_car.getText().toString();//獲取文本框的值
}
Toast.makeText(MainActivity.this, "喜歡的游戲是:" + game + " 喜歡的車是:" + car,
Toast.LENGTH_LONG).show();
}
/**
*
* 此方法描述的是: 初始化數(shù)據(jù)
*/
private void initData() {
cars = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
cars.add("寶馬-" + i);
cars.add("奔馳-" + i);
cars.add("悍馬-" + i);
cars.add("路虎-" + i);
cars.add("吉普-" + i);
cars.add("奧迪-" + i);
cars.add("福特-" + i);
cars.add("英菲尼迪-" + i);
}
}
/**
*
* 此方法描述的是: 初始化控件
*/
private void initView() {
actv_game = (AutoCompleteTextView) findViewById(R.id.actv_game);
actv_car = (AutoCompleteTextView) findViewById(R.id.actv_car);
}
}
第一個(gè)ArrayAdapter實(shí)例:
ArrayAdapter.createFromResource(context, textArrayResId, textViewResId); context Context對(duì)象 textArrayResId 數(shù)據(jù)集合ID textViewResId Layout ID
第二個(gè)ArrayAdapter實(shí)例:
new ArrayAdapter<String>(context, resource, objects); context Context對(duì)象 resource Layout ID objects 數(shù)據(jù)集合
實(shí)例化ArrayAdapter之后,setAdapter即可!
actv_game.setAdapter(gameAdapter); actv_car.setAdapter(carAdapter);
以上所述是小編給大家介紹的Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
- Android自動(dòng)獲取輸入短信驗(yàn)證碼庫(kù)AutoVerifyCode詳解
- Android AutoWrapTextView中英文排版問(wèn)題的解決方法
- Android中使用 AutoCompleteTextView 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作
- Android自動(dòng)編輯文本框(AutoCompleteTextView)使用方法詳解
- Android中AutoCompleteTextView自動(dòng)提示
- Android仿新浪微博oauth2.0授權(quán)界面實(shí)現(xiàn)代碼(2)
- android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- 關(guān)于Android HTML5 audio autoplay無(wú)效問(wèn)題的解決方案
- Android App開(kāi)發(fā)的自動(dòng)化測(cè)試框架UI Automator使用教程
- Android中AutoCompleteTextView與TextWatcher結(jié)合小實(shí)例
- Android AutoValue使用和擴(kuò)展庫(kù)
相關(guān)文章
Android 開(kāi)機(jī)應(yīng)用掃描相關(guān)總結(jié)
本篇文章只是作為指南引導(dǎo)去看PkMS,不會(huì)貼大段代碼進(jìn)行分析,更多是基于方法分析實(shí)現(xiàn)的邏輯,另外就是代碼是基于Android 11,與Android 10之前代碼有比較大的差別。2021-05-05
Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果
這篇文章主要為大家介紹了Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
五分鐘教你Android-Kotlin項(xiàng)目編寫(xiě)
本篇文章主要介紹了五分鐘教你Android-Kotlin項(xiàng)目編寫(xiě),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android?Flutter實(shí)現(xiàn)彈簧動(dòng)畫(huà)交互的示例詳解
物理模擬可以讓?xiě)?yīng)用程序的交互感覺(jué)逼真和互動(dòng),本文章實(shí)現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動(dòng)的點(diǎn)移回中心,感興趣的可以了解一下2023-04-04
解決Android Studio日志太長(zhǎng)或滾動(dòng)太快問(wèn)題
這篇文章主要介紹了解決Android Studio日志太長(zhǎng)或滾動(dòng)太快問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼
這篇文章主要介紹了Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼,調(diào)節(jié)屏幕亮度時(shí),先設(shè)置當(dāng)前activity亮度,再并保存為系統(tǒng)亮度即可,本文分別給出兩個(gè)步驟的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Android Studio 實(shí)現(xiàn)文檔注釋的快捷鍵
這篇文章主要介紹了Android Studio 實(shí)現(xiàn)文檔注釋的快捷鍵,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

