• 安卓AutoCompleteTextView 支持输入中文或拼音或拼音缩写的实现方式


    1.Activity如下

    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的参数如下:

    第1个,this 

    第2个,list: 需要传入形如 List<? extends Map<String, ?>>类型的集合对象

    第3个,该参数指定一个页面布局的ID

    第4个,该参数应该是一个String[]类型的参数,该参数决定提取Map<String,?>对象中哪些key对应的value来生成列表项

    第5个,该参数应该是一个int[]类型的参数,该参数决定填充哪些ID的组件

    2.主界面布局文件如下

    <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 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,并且不保留该控件所占位置, 以便隐藏拼音及拼音缩写

     

  • 相关阅读:
    如何强制360浏览器以极速模式打开页面
    如何解决Android SDK无法下载Package的问题(.net)
    Xamarin 安装步骤
    使用require.js和backbone实现简单单页应用实践
    HBuilder之初体验
    ClassLoader&双亲委派&类初始化过程
    List remove ConcurrentModificationException源码分析
    二十九、简谈设计模式
    二十八、Java基础--------正则表达式
    二十七、JDK1.5新特性---Annotation
  • 原文地址:https://www.cnblogs.com/xiaomimi/p/3516006.html
Copyright © 2020-2023  润新知