編輯:關於 Android 程式設計
在用戶需要輸入內容的場景裡,如果能在他敲前幾個字符時就把候選項列出來,輸入成本會大幅下降。搜索框、城市選擇、聯繫人挑選都是典型例子。AutoCompleteTextView 正是為此而生:它本質上是一個帶下拉候選列表的 EditText。
候選數據的來源很靈活,既可以是字符串數組,也可以是 List 集合。
package com.example.autocompletetext;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
private AutoCompleteTextView autoCompleteTextView = null;
// 候選數據源:這裡用城市列表做示例
private String[] CONTRY = new String[]{
"shanghai", "beijing", "guangzhou", "shenzhen", "nantong",
"anqing", "anhui", "hefei", "huainan", "dongguan", "chuzhou",
"wuxi", "suzhou", "nanjing", "kunshan", "henan", "lanfang",
"liuan", "huaibei", "fuyang", "bozhou", "suzhou", "wuhu"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
// 也可以動態組裝 List 作為數據源
ArrayList<String> list = new ArrayList<String>();
list.add("testtest");
list.add("tetsssss");
// 建立適配器:第二個參數是候選項的樣式佈局,第三個參數是數據源
// 註釋掉的 list 與 CONTRY 二者選其一即可
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.list_item, /* list */ CONTRY);
// 把適配器交給控件,補全功能即生效
autoCompleteTextView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
整個過程只有兩步關鍵操作:準備數據 + 設置 Adapter。這一點和 ListView 的思路完全一致——控件負責展示,Adapter 負責供養數據。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myText" />
</RelativeLayout>
補充一個實用屬性:android:completionThreshold 用來指定「輸入多少個字符後才觸發補全」,默認是 2。若候選數據量很大,把它調大一點能有效減少不必要的查詢。
這個佈局文件決定下拉列表中每一行的外觀:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" />
因為 ArrayAdapter 只需要一個能顯示文本的 View,這裡直接用 TextView 作為根節點即可,不必多包一層容器。
使用時需要留意幾點:
notifyDataSetChanged(),切忌在主線程發請求。AutoCompleteTextView 的「互斥」方案,或在失焦時做一次合法性校驗。AutoCompleteTextView 是「少寫代碼、多拿體驗」的典型代表:一個控件、一個 Adapter,就換來了完整的輸入補全能力。把數據源和樣式佈局這兩處按業務替換掉,它就能適配絕大多數需要候選提示的界面。
Android Bitmap 加載優化與逐像素加工實戰
一、載入與像素讀寫 Android SDK 提供了 getPixel 與 setPixel 兩個 API 讓我們直接操作像素。讀取的寫法如下: int pixel = b
Android 計時器與時間計算器的實作方法
需求是這樣的:初始顯示「00:00:00」,點擊開始後歸零並開始計時,如 10:28:34;點擊停止則暫停計時。 過程中踩過一個坑:使用 Calendar、DateFo
在 Visual Studio 中打造 wxWidgets C++ GUI 開發環境(含中文亂碼解法)
wxWidgets 是跨平台 C++ GUI 函式庫中相當經典的一個。本文示範的建置方式為 Unicode + DLL + Debug,主要考量是最後產出的 exe 不會
Android Studio NDK 入門(五):JNI 中的 Java 物件傳遞、修改與效能比較
概述 本篇處理 Java 與 C++ 之間的物件互操作問題,包含四件事:從 C++ 建立 Java 物件並回傳、在 C++ 中解析 Java 物件、修改 Java 物件的