android AutoCompleteTextView 實現輸入提示,類似百度支持輸入拼音提示中文(gray)

        android 的 AutoCompleteTextView 控件實現了輸入框的輸入提示功能,這個功能更加使用於國外的手機用戶來使用。而很多時候國人更多的是要象百度那樣我輸入的是拼音也能將中文提示出來,如:輸入xinlang  就能提示:新浪、新浪微博等。又或者是輸入:xl 拼音首字母也能做到。這樣的提示纔是更加的符合國人的習慣。

先上圖,看效果:


        先簡單的介紹下AutoCompleteTextView 控件的基本用法:這個在android的sdk裏也是有介紹的,下面是sdk中介紹的實現代碼:

public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }
從上面的代碼可到實現這個功能是很簡單的,只要將一個要提示的數組綁定到ArrayAdapter中,再將ArrayAdapter和 AutoCompleteTextView 控件綁定一起。

下面來說下我的功能的實現方式:

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


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addItems();
        AutoCompleteTextView 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);

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", "NOKIA nuojiya NJY");
    item.put( "brandName", "諾基亞");
    list.add( item );


    item = new HashMap<String,String>();
    item.put( "brandSearchText", "SVMSUN SX sanxing");
    item.put( "brandName", "三星");
    list.add( item );
   
   
    item = new HashMap<String,String>();
    item.put( "brandSearchText", "摩托羅拉  moto MTLL motuoluola motoloar");
    item.put( "brandName", "摩托羅拉");
    list.add( item );


    }


}

1.我這裏將不使用ArrayAdapter而是用SimpleAdapter。

2.用程序把所有的搜索詞語拼接成brandSearchText字段,用空格隔開。比如:“諾基亞 NOKIA  njy nuojiya”,把它作爲搜索詞。
   注意這個拼接過程應該只做一次,然後保存在靜態變量裏面,以便提高效率。因爲只要你的提示詞的一多數據量大了就會使提示的速度減慢。
還有就是這裏最好是把要提示的文本放在brandName字段 如“諾基亞”,這樣的話就不要在searchText字段中添加“諾基亞”不然提示會出現重複。
將準備好的方法放在ArrayList<HashMap<String,String>>中這裏的數據也可以是從數據庫中添加
3.然後再將數據放到SimpleAdapter 中與AutoCompleteTextView 綁定自定義一個提示顯示的listItem佈局,在佈局只用的顯示brandSearchText字段數據的textview隱藏:
<?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>
4.這樣就不會把不需要顯示的內容顯示出來。
到這裏還有最後一步,如果不做的話這裏點擊提示詞的某一行後,顯示在輸入框中的內容將會是這樣的:{brandName=三星,brandSearchText=SVMSUN SX sanxing
所以最後我們要對item的點擊事件做處理:

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());
}

});

到了這裏就基本完成了。

發佈了43 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章