服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android程序开发之获取汉字的首字母

Android程序开发之获取汉字的首字母

2021-06-24 13:40jerrylsxu Android

获取汉字首字母在android程序开发中经常用到,下面通过本文给大家介绍Android程序开发之获取汉字的首字母,需要的朋友参考下吧

获取一个汉字的拼音首字母。 gb码两个字节分别减去160,转换成10进制码组合就可以得到区位码例如汉字“你”的gb码是0xc4/0xe3,分别减去0xa0(160)就是0x24/0x430x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n'。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".mainactivity" >
<edittext
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入汉字" >
</edittext>
<button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" >
</button>
<textview
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</textview>
</linearlayout>
public class mainactivity extends activity {
static final int gb_sp_diff = 160;
// 存放国标一级汉字不同读音的起始区位码
static final int[] secposvaluelist = { 1601, 1637, 1833, 2078, 2274, 2302,
2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027,
4086, 4390, 4558, 4684, 4925, 5249, 5600 };
// 存放国标一级汉字不同读音的起始区位码对应读音
static final char[] firstletter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x',
'y', 'z' };
private edittext edit;
private textview text;
private button button;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
edit = (edittext) this.findviewbyid(r.id.edit);
text = (textview) this.findviewbyid(r.id.textview);
button = (button) this.findviewbyid(r.id.button);
button.setonclicklistener(buttonlistener);
text.settext("拼音");
}
private view.onclicklistener buttonlistener = new view.onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
if (v == button) {
string characters = edit.gettext().tostring();
string spells = getspells(characters);
text.settext(spells);
}
}
};
public static string getspells(string characters) {
stringbuffer buffer = new stringbuffer();
for (int i = 0; i < characters.length(); i++) {
char ch = characters.charat(i);
if ((ch >> 7) == 0) {
// 判断是否为汉字,如果左移7为为0就不是汉字,否则是汉字
} else {
char spell = getfirstletter(ch);
buffer.append(string.valueof(spell));
}
}
return buffer.tostring();
}
// 获取一个汉字的首字母
public static character getfirstletter(char ch) {
byte[] unicode = null;
try {
unicode = string.valueof(ch).getbytes("gbk");
} catch (unsupportedencodingexception e) {
e.printstacktrace();
return null;
}
if (unicode[0] < 128 && unicode[0] > 0) { // 非汉字
return null;
} else {
return convert(unicode);
}
}
/**
* 获取一个汉字的拼音首字母。 gb码两个字节分别减去160,转换成10进制码组合就可以得到区位码
* 例如汉字“你”的gb码是0xc4/0xe3,分别减去0xa0(160)就是0x24/0x43
* 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n'
*/
static char convert(byte[] bytes) {
char result = '-';
int secposvalue = 0;
int i;
for (i = 0; i < bytes.length; i++) {
bytes[i] -= gb_sp_diff;
}
secposvalue = bytes[0] * 100 + bytes[1];
for (i = 0; i < 23; i++) {
if (secposvalue >= secposvaluelist[i]
&& secposvalue < secposvaluelist[i + 1]) {
result = firstletter[i];
break;
}
}
return result;
}
}

以上所述是小编给大家介绍的android程序开发之获取汉字的首字母,希望对大家有所帮助!

延伸 · 阅读

精彩推荐