安卓AutoCompleteTextView 支持輸入中文或拼音或拼音縮寫的實現方式

1.Activity以下html

package com.travelsky.autotest;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AutoTestActivity extends Activity {
	/** Called when the activity is first created. */
	ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
	AutoCompleteTextView ac;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auto_test);
		addItems();
		ac = (AutoCompleteTextView) findViewById(R.id.autocomplete);
		SimpleAdapter notes = new SimpleAdapter(this, list,
				R.layout.main_item_three_line_row, new String[] {
						"brandSearchText", "brandName" }, new int[] {
						R.id.searchText, R.id.brandName });
		ac.setAdapter(notes);
		ac.setThreshold(1);
          

          //如下代碼是爲了格式化點擊提示選項後最後顯示的內容,不然會把Map的key和value都顯示出來 ac.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { TextView tv = (TextView)arg1.findViewById(R.id.brandName); ac.setText(tv.getText().toString()+" "); ac.setSelection((ac.getText().toString()).length()); } }); } private void addItems() { HashMap<String, String> item; item = new HashMap<String, String>(); item.put("brandSearchText", "dongfanghangkonggongsi DFHKGS dfhkgs"); item.put("brandName", "東方航空公司"); list.add(item); item = new HashMap<String, String>(); item.put("brandSearchText", "nanfanghangkonggongsi NFHKGS nfhkgs"); item.put("brandName", "南方航空公司"); list.add(item); item = new HashMap<String, String>(); item.put("brandSearchText", "guojihangkonggongsi GJHKGS gjhkgs"); item.put("brandName", "國際航空公司"); list.add(item); item = new HashMap<String, String>(); item.put("brandSearchText", "sichuanhangkonggongsi SCHKGS schkgs"); item.put("brandName", "四川航空公司"); list.add(item); } }

其中,SimpleAdapter的參數以下:java

第1個,this android

第2個,list: 須要傳入形如 List<? extends Map<String, ?>>類型的集合對象app

第3個,該參數指定一個頁面佈局的IDide

第4個,該參數應該是一個String[]類型的參數,該參數決定提取Map<String,?>對象中哪些key對應的value來生成列表項佈局

第5個,該參數應該是一個int[]類型的參數,該參數決定填充哪些ID的組件this

 

2.主界面佈局文件以下spa

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="航空公司:"
	    android:textSize="20sp"/>
	<AutoCompleteTextView 
	    android:id="@+id/autocomplete"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:hint="請輸入航空公司"
	    />
			
</LinearLayout>

 

3.SimpleAdapter使用的自定義佈局文件以下xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:id="@+id/brandName" />
	
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:id="@+id/searchText"
		android:visibility="gone" />		
</LinearLayout>

其中,android:visibility="gone" 表示隱藏TextView,而且不保留該控件所佔位置, 以便隱藏拼音及拼音縮寫htm

相關文章
相關標籤/搜索