Android提供了汉字转拼音的类,这个类是在联系人app下的。具体目录是packages/providers/ContactsProvider/src/com/android/providers/contacts的HanziToPinyin.java文件。如果想要这个工具类,可以直接拷贝来用。
点击(此处)折叠或打开
- package nd.ray.hanzi2pinyin;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.text.Collator;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Locale;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import nd.ray.hanzi2pinyin.HanziToPinyin.Token;
- public class FMain extends Activity implements View.OnClickListener{
- private TextView edHanzi = null;
- private TextView edPinYin = null;
- private TextView edPY = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fmain);
- Button btn = (Button)findViewById(R.id.btnConvert);
- btn.setOnClickListener(this);
- edHanzi = (TextView)findViewById(R.id.edHanzi);
- edPinYin = (TextView)findViewById(R.id.edPinyin);
- edPY = (TextView)findViewById(R.id.edFirstPY);
- PatternStrStr();
- PatternSplit();
- PatternReplace();
- }
- private String getFullPinYin(String source) {
- if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.CHINESE)) {
- return source;
- }
- ArrayList<HanziToPinyin.Token> tokens = HanziToPinyin.getInstance().get(source);
- if (tokens == null || tokens.size() == 0) {
- return source;
- }
- StringBuffer result = new StringBuffer();
- for (HanziToPinyin.Token token : tokens) {
- if (token.type == HanziToPinyin.Token.PINYIN) {
- result.append(token.target);
- } else {
- result.append(token.source);
- }
- }
- return result.toString();
- }
- public String getFirstPinYin(String source) {
- if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.CHINESE)) {
- return source;
- }
- ArrayList<HanziToPinyin.Token> tokens = HanziToPinyin.getInstance().get(source);
- if (tokens == null || tokens.size() == 0) {
- return source;
- }
- StringBuffer result = new StringBuffer();
- for (HanziToPinyin.Token token : tokens) {
- if (token.type == HanziToPinyin.Token.PINYIN) {
- result.append(token.target.charAt(0));
- } else {
- result.append("#");
- }
- }
- return result.toString();
- }
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.btnConvert) {
- String szHanzi = edHanzi.getText().toString();
- String szPinYin = getFullPinYin(szHanzi);
- String szPY = getFirstPinYin(szHanzi);
- edPinYin.setText(szPinYin);
- edPY.setText(szPY);
- }
- }
- /*
- * 正则表达式
- * */
- //在字符串包含
- private boolean PatternStrStr()
- {
- Pattern pattern = Pattern.compile("正则表达式");
- Matcher matcher = pattern.matcher("要检测正则表达式是否出现的源串");
- return matcher.matches();
- }
- //分割字符串
- private String[] PatternSplit()
- {
- Pattern pattern = Pattern.compile("[, |]+"); //正则表达式
- String[] strs = pattern.split("Java Hello World Java,Hello,,World|SUN");
- return strs;
- }
- //文字替换
- private void PatternReplace()
- {
- Pattern pattern = Pattern.compile("要替换的内容");
- Matcher matcher = pattern.matcher("包含要替换的内容的源串, 可以包含要替换的内容多次");
- //String szResult = matcher.replaceFirst("要换成的新内容");
- String szResult = matcher.replaceAll("要换成的新内容");
- System.out.println(szResult);
- }
- }