2. [代码]配置文件
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | > |
07 | < EditText |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:id = "@+id/edit" |
11 | android:hint = "输入汉字" |
12 | ></ EditText > |
13 | < Button |
14 | android:layout_width = "fill_parent" |
15 | android:layout_height = "wrap_content" |
16 | android:id = "@+id/button" |
17 | android:text = "@string/button" |
18 | ></ Button > |
19 | < TextView |
20 | android:id = "@+id/textView" |
21 | android:layout_width = "fill_parent" |
22 | android:layout_height = "wrap_content" |
23 | android:textColor = "@color/white" |
24 | ></ TextView > |
25 | </ LinearLayout > |
3. [代码]Java代码
01 | package com.android.antking.getspell; |
02 | import java.io.UnsupportedEncodingException; |
03 |
04 | import android.app.Activity; |
05 | import android.os.Bundle; |
06 | import android.view.View; |
07 | import android.widget.Button; |
08 | import android.widget.EditText; |
09 | import android.widget.TextView; |
10 | import android.widget.Toast; |
11 | public class MainActivity extends Activity { |
12 | static final int GB_SP_DIFF= 160 ; |
13 | // 存放国标一级汉字不同读音的起始区位码 |
14 |
|
15 | 3472 , 3635 , 3722 , 3730 , 3858 , 4027 , 4086 , 4390 , 4558 , 4684 , 4925 , |
16 | 5249 , 5600 }; |
17 | // 存放国标一级汉字不同读音的起始区位码对应读音 |
18 |
|
19 | private EditText edit; |
20 | private TextView text; |
21 | private Button button; |
22 | /** Called when the activity is first created. */ |
23 | @Override |
24 | public void onCreate(Bundle savedInstanceState) { |
25 | super .onCreate(savedInstanceState); |
26 | setContentView(R.layout.main); |
27 | edit = (EditText) this .findViewById(R.id.edit); |
28 | text = (TextView) this .findViewById(R.id.textView); |
29 | button = (Button) this .findViewById(R.id.button); |
30 | button.setOnClickListener(buttonListener); |
31 | text.setText( "拼音" ); |
32 | } |
33 | private View.OnClickListener buttonListener = new View.OnClickListener() { |
34 | |
35 | @Override |
36 | public void onClick(View v) { |
37 | // TODO Auto-generated method stub |
38 | if (v==button){ |
39 | String characters = edit.getText().toString(); |
40 | String spells = getSpells(characters); |
41 | text.setText(spells); |
42 | } |
43 | } |
44 | }; |
45 | |
46 | public static String getSpells(String characters){ |
47 | StringBuffer buffer = new StringBuffer(); |
48 | for ( int i= 0 ;i<characters.length();i++){ |
49 | |
50 | char ch = characters.charAt(i); |
51 | if ((ch>> 7 )== 0 ){ |
52 | //判断是否为汉字,如果左移7为为0就不是汉字,否则是汉字 |
53 | } else { |
54 | char spell = getFirstLetter(ch); |
55 | buffer.append(String.valueOf(spell)); |
56 | } |
57 | } |
58 | return buffer.toString(); |
59 | } |
60 | // 获取一个汉字的首字母 |
61 | public static Character getFirstLetter( char ch) { |
62 | |
63 | byte [] uniCode = null ; |
64 | try { |
65 | uniCode = String.valueOf(ch).getBytes( "GBK" ); |
66 | } catch (UnsupportedEncodingException e) { |
67 | e.printStackTrace(); |
68 | return null ; |
69 | } |
70 | if (uniCode[ 0 ] < 128 && uniCode[ 0 ] > 0 ) { // 非汉字 |
71 | return null ; |
72 | } else { |
73 | return convert(uniCode); |
74 | } |
75 | } |
76 | /** |
77 | * 获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码 |
78 | * 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43 |
79 | * 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n’ |
80 | */ |
81 | static char convert( byte [] bytes) { |
82 | char result = '-' ; |
83 | int secPosValue = 0 ; |
84 | int i; |
85 | for (i = 0 ; i < bytes.length; i++) { |
86 | bytes[i] -= GB_SP_DIFF; |
87 | } |
88 | secPosValue = bytes[ 0 ] * 100 + bytes[ 1 ]; |
89 | for (i = 0 ; i < 23 ; i++) { |
90 | if (secPosValue >= secPosValueList[i] && secPosValue < secPosValueList[i + 1 ]) { |
91 | result = firstLetter[i]; |
92 | break ; |
93 | } |
94 | } |
95 | return result; |
96 | } |
97 | } |