Android绝黑剑之AutoCompleteTextView、MultiAutoCompleteTextView之智能联想

传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229


黑剑

        黄蓉见那汉子左手使柄金光闪闪的大刀,右手使柄又细又长的黑剑,招数奇幻,生平未见,自己若不出手,武氏兄弟便要遭逢奇险,向李莫愁道:“那两个少年是我徒儿。”李莫愁涩然一笑,心想:“他们母亲是我杀的,我岂不知?”见那中年汉子武功高得出奇,江湖上却从未听说有这号人物,心下暗自惊异,微微一笑,道:“下场罢!”拔出拂尘一拂,黄蓉也已持竹棒在手。两人左右齐上,李莫愁拂尘攻那人黑剑,黄蓉的竹棒便缠向他金刀。

        今天我们学习如何利用Android平台“黑剑”AutoCompleteTextView、MultiAutoCompleteTextView来实现自动完成对单个和多个关键字的内容查询(类似百度搜索关键字自动查找内容),下面给出该情景的案例:

1案例技术要点

(1)android.widget.AutoCompleteTextView:对单个关键字搜索实现内容自动提示功能。
(2)android.widget.MultiAutoCompleteTextView:对一组关键字搜索实现内容自动提示功能。关键字之间用英文字符“,”分隔。

2案例代码陈列

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.autocompletetextview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AutoCompleteTextViewMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml

<resources>
    <string name="app_name">AutoCompleteTextView、MultiAutoCompleteTextView自动完成对单个和多个关键字的内容查询</string>
    <string name="tv1">AutoCompleteTextView</string>
    <string name="tv2">MultiAutoCompleteTextView</string>
</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv1" />
    <AutoCompleteTextView android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv2" />
    <MultiAutoCompleteTextView android:id="@+id/multiauto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

AutoCompleteTextViewMainActivity.java

package com.android.autocompletetextview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

/**
 * 实现用户输入的内容有自动提示的功能(类似百度搜索结果)
 * AutoCompleteTextView案例:实现关键字的模糊查询
 * @author lynnli1229
 */
public class AutoCompleteTextViewMainActivity extends Activity {
    //支持单个关键字自动查询,继续输入其他关键字会覆盖现有的
    private AutoCompleteTextView auto;
    //支持多个关键字自动查询,用英文“,”隔开
    private MultiAutoCompleteTextView multiAuto;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        auto = (AutoCompleteTextView) findViewById(R.id.auto);
        String[] autoStrings = new String[]{"中国人", "中国人真爷们", "中国人好棒哦", "Google", "Goodbye"};
        //ArrayAdapter构造器第二个参数定制适配器的下拉风格
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, autoStrings); 
        auto.setAdapter(adapter);
        
        multiAuto = (MultiAutoCompleteTextView) findViewById(R.id.multiauto);
        multiAuto.setAdapter(adapter);
        multiAuto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

} 

3案例效果展示

 
posted @ 2013-05-27 22:22  Innosight  阅读(214)  评论(0编辑  收藏  举报