欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a

 更新時(shí)間:2016年07月15日 10:44:53   作者:y_xing  
這篇文章主要介紹了Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

 自動(dòng)提示文本框(AutoCompleteTextView)可以加強(qiáng)用戶體驗(yàn),縮短用戶的輸入時(shí)間(百度的搜索框就是這個(gè)效果)。

先給大家展示下效果圖,如果大家感覺還不錯(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è)字符之后開始檢索。一般設(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論