您的位置:首页 > 移动开发 > Android开发

Android-汉字转拼音列表

2016-06-17 10:52 423 查看
前言:

Android对于汉字转拼音,网络上多少有着一些三方控件,自我感觉用着不是很顺,因此就自己综合了一下网络上各路牛人的技术,以年轻的技术理念给一些需要的人帮助,同时也是给自己一个锻炼的机会。

思路:

ListView+AlphabetView(自定义字母控件),AlphabetView滑动带动ListView显示对应的字母对应列表,对于ListView中显示字母其实有一个问题就是,例如:“重庆”-->“zhongqing”问题,此处我借鉴了网络上的一个多音字处理,利用Polyphone类进行多音字进行过滤处理。汉字转拼音类也是借鉴网络上的大牛的,只是对代码进行了部分的修改,让你们见笑了,自己太懒!好了言归正传,源码什么之类的我就直接弄在下面了,因为考虑到编程工具类型不同,自己复制对应代码就OK了,关于代码这种东西,我喜欢简单,你看着简单,用起来也简单,思路清晰,所以我就把对应类用连接来表示,要源码就直接在链接看就行了。

代码实现:

对于此功能的实现需要准备如下类:AlphabetComparator(对拼音列表进行排序处理)、 CharacterParser(汉字转拼音的类)、
Polyphone(多音字过滤的类)、AlphabetView (字母选择列表类)

AlphabetComparator:http://note.youdao.com/yws/public/redirect/share?id=a0a7070c7af42c2679a35a0da5f620ca&type=false

CharacterParser:http://note.youdao.com/yws/public/redirect/share?id=4f35a1aaa49d9305ff23c96cc98bd99f&type=false

Polyphone:http://note.youdao.com/yws/public/redirect/share?id=b62655cb82cc281e2b79b33d451689f7&type=false

AlphabetView: http://note.youdao.com/yws/public/redirect/share?id=39b026b6951f4b247b737ff90e65dedd&type=false

Activity中使用:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

private ListView listView;
private AlphabetView alphabetView;

private AlphabetsAdapter adapter;
private List<Map<String, String>> alphabetList;
private String[] names = {"哈佛和恢复","埃及高科技高","布局", "酒精发酵", "喜欢重庆市", "重庆很好", "你好啊", "重量", "体重", "的健", "流量计费加", "附近解放", "发奖金", "让姐夫", "已发货", "昂见附", "行人", "行长"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
listView = (ListView) findViewById(R.id.listview);
alphabetView = (AlphabetView) findViewById(R.id.alphableview);
//初始化数据
alphabetList = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
Map<String, String> map = new HashMap<>();
map.put("name", names[i]);
String alpha = CharacterParser.getInstance().parseChineseToPinyin(names[i]).substring(0, 1).toUpperCase();
if (alpha.matches("[A-Z]")) {
map.put("alphabet", alpha);
} else {
map.put("alphabet", "#");
}
alphabetList.add(map);
}
//对数据进行排序
Collections.sort(alphabetList, new AlphabetComparator());
adapter = new AlphabetsAdapter(alphabetList);
listView.setAdapter(adapter);
//对字母列表进行监听
alphabetView.setOnClickListener(new AlphabetView.OnClickListener() {
@Override
public void onClick(String[] alphabets, String text, int position) {
listView.setSelection(getListFirstAlphabetPosition(alphabetList, text));
}
});
alphabetView.setOnTouchMoveListener(new AlphabetView.OnTouchMoveListerner() {
@Override
public void OnTouchMove(String[] alphabets, String text, int position) {
listView.setSelection(getListFirstAlphabetPosition(alphabetList, text));
}
});
}

/**
* 获取ListView中第一个字母对应的位置
* @param list
* @param alphabet
* @return
*/
private int getListFirstAlphabetPosition(List<Map<String, String>> list, String alphabet) {
int position = 0;
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = list.get(i);
if (map.get("alphabet").equals(alphabet)) {
position = i;
break;
}
}
return position;
}

private class AlphabetsAdapter extends BaseAdapter {

private List<Map<String, String>> alphabets;

public AlphabetsAdapter(List<Map<String, String>> alphabets) {
this.alphabets = alphabets;
}

public void notifyAlpahbetDataSetChanged(List<Map<String, String>> alphabets) {
this.alphabets = alphabets;
notifyDataSetChanged();
}

@Override
public int getCount() {
return alphabets == null ? 0 : alphabets.size();
}

@Override
public Object getItem(int position) {
return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item_alphabet, parent, false);
holder.tv_alphabet = (TextView) convertView.findViewById(R.id.tv_alphabet);
holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_alphabet.setVisibility(getListFirstAlphabetPosition(alphabets, alphabets.get(position).get("alphabet"))==position?View.VISIBLE:View.GONE);
String name = alphabets.get(position).get("name");
String alphabet = alphabets.get(position).get("alphabet");
holder.tv_alphabet.setText(alphabet);
holder.tv_content.setText(name);
return convertView;
}

private class ViewHolder {
private TextView tv_alphabet;
private TextView tv_content;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息