获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x430x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n'。
案例演示:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 <EditText 8 android:id="@+id/edit" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:hint="输入汉字" > 12 </EditText> 13 <Button 14 android:id="@+id/button" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="button" > 18 </Button> 19 <TextView 20 android:id="@+id/textView" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" > 23 </TextView> 24 </LinearLayout> 25 public class MainActivity extends Activity { 26 static final int GB_SP_DIFF = 160; 27 // 存放国标一级汉字不同读音的起始区位码 28 static final int[] secPosValueList = { 1601, 1637, 1833, 2078, 2274, 2302, 29 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 30 4086, 4390, 4558, 4684, 4925, 5249, 5600 }; 31 // 存放国标一级汉字不同读音的起始区位码对应读音 32 static final char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 33 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 34 'y', 'z' }; 35 private EditText edit; 36 private TextView text; 37 private Button button; 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 edit = (EditText) this.findViewById(R.id.edit); 43 text = (TextView) this.findViewById(R.id.textView); 44 button = (Button) this.findViewById(R.id.button); 45 button.setOnClickListener(buttonListener); 46 text.setText("拼音"); 47 } 48 private View.OnClickListener buttonListener = new View.OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 // TODO Auto-generated method stub 52 if (v == button) { 53 String characters = edit.getText().toString(); 54 String spells = getSpells(characters); 55 text.setText(spells); 56 } 57 } 58 }; 59 public static String getSpells(String characters) { 60 StringBuffer buffer = new StringBuffer(); 61 for (int i = 0; i < characters.length(); i++) { 62 char ch = characters.charAt(i); 63 if ((ch >> 7) == 0) { 64 // 判断是否为汉字,如果左移7为为0就不是汉字,否则是汉字 65 } else { 66 char spell = getFirstLetter(ch); 67 buffer.append(String.valueOf(spell)); 68 } 69 } 70 return buffer.toString(); 71 } 72 // 获取一个汉字的首字母 73 public static Character getFirstLetter(char ch) { 74 byte[] uniCode = null; 75 try { 76 uniCode = String.valueOf(ch).getBytes("GBK"); 77 } catch (UnsupportedEncodingException e) { 78 e.printStackTrace(); 79 return null; 80 } 81 if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字 82 return null; 83 } else { 84 return convert(uniCode); 85 } 86 } 87 /** 88 * 获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码 89 * 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43 90 * 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n' 91 */ 92 static char convert(byte[] bytes) { 93 char result = '-'; 94 int secPosValue = 0; 95 int i; 96 for (i = 0; i < bytes.length; i++) { 97 bytes[i] -= GB_SP_DIFF; 98 } 99 secPosValue = bytes[0] * 100 + bytes[1]; 100 for (i = 0; i < 23; i++) { 101 if (secPosValue >= secPosValueList[i] 102 && secPosValue < secPosValueList[i + 1]) { 103 result = firstLetter[i]; 104 break; 105 } 106 } 107 return result; 108 } 109 }