Android program Development Gets the first letter of Chinese characters _android

Source: Internet
Author: User
Tags comparison table stringbuffer

The

Gets the phonetic first letter of a Chinese character. GB code two bytes respectively minus 160, converted into 10 code combination can get location code such as Chinese characters "You" GB code is 0XC4/0XE3, respectively minus 0xa0 (160) is 0x24/0x430x24 into 10 into the system is 36,0x43 is 67, Then its location code is 3667, in the comparison table pronunciation is ' n '.

<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:o" rientation= "Vertical" tools:context= ". Mainactivity "> <edittext android:id=" @+id/edit "android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "android:hint=" input Chinese character "> </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;//Store The starting area of the different pronunciations of the Chinese national standard Bit code static final int[] Secposvaluelist = {1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 37 22, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600}; The initial location code of the different pronunciations of the Chinese national standard corresponds to the pronunciation 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 ("pinyin"); Private View.onclicklistener Buttonlistener = new View.onclicklistener () {@Override public void OnClick (View v) {//To
Do 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) {//Judge whether it is Chinese character, if the left 7 is 0 is not Chinese character, otherwise it is Chinese character} else {char spell = GE
Tfirstletter (CH);
Buffer.append (string.valueof (spell));
} return buffer.tostring (); //Get the first letter of a Chinese character 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) {//non-kanji return null;} else {return convert (UniCode);}} /** * Gets the phonetic first letter of a Chinese character. GB code two bytes respectively minus 160, converted into 10 code combination can get location code * such as Chinese characters "You" GB code is 0XC4/0XE3, respectively minus 0xa0 (160) is 0x24/0x43 * 0x24 turn into 10 is 36,0x43 is 67, Then its location code is 3667, in the comparison table pronunciation is ' 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] * + bytes[1]; for (i = 0; i < i++) {if (Secposvalue >= secposvaluelist[i] &&Amp
Secposvalue < Secposvaluelist[i + 1] {result = Firstletter[i];
return result; }
}

The above is a small set to introduce the Android program to obtain the first letter of Chinese characters, I hope to help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.