Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android程序開發——簡單控件之AutoCompleteTextView的用法

Android程序開發——簡單控件之AutoCompleteTextView的用法

編輯:關於Android編程

1.初始使用

android:completionThreshold="1"的作用是提示功能從第一個字母開始
package com.treasure_ct.study_demo.simplecontrols;

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

import com.treasure_ct.study_demo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by treasure on 2016.08.27.
 */
public class AutoCompleteActivity extends Activity{
    private AutoCompleteTextView autoCompleteTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simplecontrols_autocomplete);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        List list = new ArrayList<>();
        list.add("String");
        list.add("Integer");
        list.add("Float");
        list.add("Short");
        list.add("Double");
        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);
        autoCompleteTextView.setAdapter(adapter);
    }
}
效果\

 

 

 

 

2.實現可以使用拼音查找出對應的漢語


1)數據源格式 類

package com.treasure_ct.study_demo.simplecontrols;

/**
 * Created by treasure on 2016.08.28.
 */
public class AutoEntry {
    private String text;
    private String pinyin;

    public AutoEntry(String text, String pinyin) {
        this.text = text;
        this.pinyin = pinyin;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    @Override
    public String toString() {
        return text+"("+pinyin+ ")";
    }
}
2)定義適配器 類
package com.treasure_ct.study_demo.simplecontrols;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by treasure on 2016.08.28.
 */
public class AutoAdapter extends BaseAdapter implements Filterable{
    private Context context;
    private Listlist;
    private Listbackup;
    private Filter filter;

    public AutoAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null){
            view = new TextView(context);
        }
        ((TextView) view).setText(list.get(i).getText());
        return view;
    }

    @Override
    public Filter getFilter() {
        if (filter == null){
            filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {
                    if (backup == null){
                        backup = new ArrayList<>(list);
                    }
                    FilterResults results = new FilterResults();
                    if (!TextUtils.isEmpty(charSequence)) {
                        List entries = new ArrayList<>();
                        for (AutoEntry entry : backup) {
                            String pinyin = entry.getPinyin();
                            if (pinyin.contains(charSequence.toString())){
                                entries.add(entry);
                            }
                        }
                        results.count = entries.size();
                        results.values = entries;
                    }else {
                        results.count = 0;
                        results.values = new ArrayList<>();
                    }
                    return results;
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                    list.clear();
                    list.addAll((Collection)filterResults.values);
                    notifyDataSetChanged();
                }
            };
        }
    return filter   ;
    }
}
3)調用類
package com.treasure_ct.study_demo.simplecontrols;

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

import com.treasure_ct.study_demo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by treasure on 2016.08.27.
 */
public class AutoCompleteActivity extends Activity{
    private AutoCompleteTextView autoCompleteTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simplecontrols_autocomplete);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
//        List list = new ArrayList<>();
        List list = new ArrayList<>();
        list.add(new AutoEntry("北京","beijing"));
        list.add(new AutoEntry("山東","shandong"));
        list.add(new AutoEntry("上海","shanghai"));
        list.add(new AutoEntry("廣東","guangdong"));
        list.add(new AutoEntry("浙江","zhejiang"));
        list.add(new AutoEntry("江蘇","jiangsu"));
        list.add(new AutoEntry("天津","tianjin"));
        list.add(new AutoEntry("遼寧","liaoning"));
        list.add(new AutoEntry("山西","shanxi"));
        list.add(new AutoEntry("河南","henan"));
        list.add(new AutoEntry("河北","hebei"));
        list.add(new AutoEntry("四川","sichuan"));
        AutoAdapter autoAdapter = new AutoAdapter(this,list);

//        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);
        autoCompleteTextView.setAdapter(autoAdapter);
    }
}

效果圖:

\\

 

3.通過系統自帶的拼音jar包來通過拼音識別漢字(list只添加漢字,需要導入一個jpinyin的jar包)

自定義adapter類

package com.treasure_ct.study_demo.simplecontrols;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by treasure on 2016.08.28.
 */
public class AutoStartWithAdapter extends BaseAdapter implements Filterable{
    private Context context;
    private List list;
    private List backup;
    private Filter filter;

    public AutoStartWithAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null){
            view = new TextView(context);
        }
        ((TextView) view).setText(list.get(i));
        return view;
    }

    @Override
    public Filter getFilter() {
        if (filter == null){
            filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {
                    if (backup == null){
                        backup = new ArrayList<>(list);
                    }
                    FilterResults results = new FilterResults();
                    if (!TextUtils.isEmpty(charSequence)){
                        List strings = new ArrayList<>();
                        for (String str : backup) {
                            try {
                                String string = PinyinHelper.convertToPinyinString(str, "", PinyinFormat.WITHOUT_TONE);
                                if (string.contains(charSequence)){
                                    strings.add(str);
                                }else {
                                    String shortPinyin = PinyinHelper.getShortPinyin(str);
                                    if (shortPinyin.startsWith(charSequence.toString())) {
                                        strings.add(str);
                                    }
                                }
                            } catch (PinyinException e) {
                                e.printStackTrace();
                            }
                            results.count = strings.size();
                            results.values = strings;
                        }
                    }else {
                        results.count = 0;
                        results.values = new ArrayList<>();
                    }
                    return results;
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                    list.clear();
                    list.addAll((Collection)filterResults.values);
                    notifyDataSetChanged();
                }
            };
        }
        return filter;
    }
}

 

主類
package com.treasure_ct.study_demo.simplecontrols;

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

import com.treasure_ct.study_demo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by treasure on 2016.08.27.
 */
public class AutoCompleteActivity extends Activity{
    private AutoCompleteTextView autoCompleteTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simplecontrols_autocomplete);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
//        List list = new ArrayList<>();
//        List list = new ArrayList<>();
//        list.add(new AutoEntry("北京","beijing"));
//        list.add(new AutoEntry("山東","shandong"));
//        list.add(new AutoEntry("上海","shanghai"));
//        list.add(new AutoEntry("廣東","guangdong"));
//        list.add(new AutoEntry("浙江","zhejiang"));
//        list.add(new AutoEntry("江蘇","jiangsu"));
//        list.add(new AutoEntry("天津","tianjin"));
//        list.add(new AutoEntry("遼寧","liaoning"));
//        list.add(new AutoEntry("山西","shanxi"));
//        list.add(new AutoEntry("河南","henan"));
//        list.add(new AutoEntry("河北","hebei"));
//        list.add(new AutoEntry("四川","sichuan"));
        ArrayList list = new ArrayList<>();
        list.add("北京");
        list.add("上海");
        list.add("山東");
        list.add("廣東");
        list.add("浙江");
        list.add("江蘇");
        list.add("天津");
        list.add("遼寧");
        list.add("山西");
        list.add("河南");
        list.add("河北");
        list.add("四川");
//        AutoAdapter autoAdapter = new AutoAdapter(this,list);
        AutoStartWithAdapter autoStartWithAdapter = new AutoStartWithAdapter(this,list);
//        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);
        autoCompleteTextView.setAdapter(autoStartWithAdapter);
    }
}


4.多行自動填補
package com.treasure_ct.study_demo.simplecontrols;

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

import com.treasure_ct.study_demo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by treasure on 2016.08.27.
 */
public class AutoCompleteActivity extends Activity{
    private AutoCompleteTextView autoCompleteTextView;
    private MultiAutoCompleteTextView multiAutoCompleteTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simplecontrols_autocomplete);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiautoComplete);

//        List list = new ArrayList<>();
//        List list = new ArrayList<>();
//        list.add(new AutoEntry("北京","beijing"));
//        list.add(new AutoEntry("山東","shandong"));
//        list.add(new AutoEntry("上海","shanghai"));
//        list.add(new AutoEntry("廣東","guangdong"));
//        list.add(new AutoEntry("浙江","zhejiang"));
//        list.add(new AutoEntry("江蘇","jiangsu"));
//        list.add(new AutoEntry("天津","tianjin"));
//        list.add(new AutoEntry("遼寧","liaoning"));
//        list.add(new AutoEntry("山西","shanxi"));
//        list.add(new AutoEntry("河南","henan"));
//        list.add(new AutoEntry("河北","hebei"));
//        list.add(new AutoEntry("四川","sichuan"));
        ArrayList list = new ArrayList<>();
        list.add("北京");
        list.add("上海");
        list.add("山東");
        list.add("廣東");
        list.add("浙江");
        list.add("江蘇");
        list.add("天津");
        list.add("遼寧");
        list.add("山西");
        list.add("河南");
        list.add("河北");
        list.add("四川");
//        AutoAdapter autoAdapter = new AutoAdapter(this,list);
        AutoStartWithAdapter autoStartWithAdapter = new AutoStartWithAdapter(this,list);
//        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);
        autoCompleteTextView.setAdapter(autoStartWithAdapter);
        multiAutoCompleteTextView.setAdapter(autoStartWithAdapter);
        MultiAutoCompleteTextView.CommaTokenizer commaTokenizer = new MultiAutoCompleteTextView.CommaTokenizer();
        commaTokenizer.terminateToken(",");
        multiAutoCompleteTextView.setTokenizer(commaTokenizer);
    }
}


\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved