关闭

拼音自动补全第三方库jpinyin

88人阅读 评论(0) 收藏 举报
分类:

这里写图片描述

Android Studio 里添加Library jpinyin

写自动补全适配器。输入拼音能自动补全汉字

package com.example.hasee.autotest828;

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 hasee on 2016/8/28.
 */
public class jpinyinAdapter extends BaseAdapter implements Filterable {
    private Context context;
    private List<String> list;
    private List<String> backup;
    private Filter filter;

    public jpinyinAdapter(Context context, List<String> 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 jpinyinFilter();
        }
        return filter;
    }

    public class jpinyinFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            if (backup == null) {
                backup = new ArrayList<>(list);//list里的数据进行备份
            }
            FilterResults results = new FilterResults();
            if (!TextUtils.isEmpty(charSequence)) {
                List<String> strings = new ArrayList<>();
                for (String s : backup) {
                    try {
                        //通过jpinyin里的方法 传入汉字 得到拼音。
                        String s1 = PinyinHelper.convertToPinyinString(s, "", PinyinFormat.WITHOUT_TONE);//分隔符为空
                        if (s1.contains(charSequence)) {//判断拼音是否包含输入的字符相匹配
                            strings.add(s);
                        } else {
                            String pinyin = PinyinHelper.getShortPinyin(s);
                            if (pinyin.startsWith(charSequence.toString())) {//首字母
                                strings.add(s);
                            }
                        }
                    } 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清空所以要先备份
            list.addAll((Collection<? extends String>) filterResults.values);
            notifyDataSetChanged();
        }
    }
}

加载适配器

   autoText = (AutoCompleteTextView) findViewById(R.id.main_auto);
        List<String> list = new ArrayList<>();
        list.add("北京");
        list.add("河南");
        list.add("郑州");
        list.add("广州");
        list.add("深圳");
        list.add("周口");
        list.add("杭州");
        jpinyinAdapter adapter = new jpinyinAdapter(this, list);
        autoText.setAdapter(adapter);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15614次
    • 积分:750
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条
    文章分类