Enter Chinese Characters in Android to get pinyin

Source: Internet
Author: User

Some time ago, I studied the android address book and found it in its contacts2.db database.Raw_contactsThe sort_key column in the table is used for sorting of Chinese pinyin and other information. It suddenly comes to mind that we can use it to get the corresponding Chinese pinyin.

 

Sorry, my simulator cannot input Chinese characters. If it is a Chinese name named "Zhang San", it should be stored in the form of "Zhang zhangsan III"

My specific ideas are as follows:

1. Insert Chinese characters into the table

2. Look up the table and find this field. After corresponding processing, the output

3. Delete this field

 

There are many tables in contacts2.db. For more information, please download the project at the end of this article, which contains the project in this article. Check the small software of the contacts2.db database and relevant tutorials! (PS: To export contacts2.db, the simulator must be enabled first)

 

In this case, you need to perform read and write operations on the address book, so add the corresponding permissions to androidmanifest. xml:

<uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" /> 

 

Paste the Code:

Package COM. pinyin; import android. app. activity; import android. content. contenturis; import android. content. contentvalues; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. provider. contactscontract; import android. provider. contactscontract. commondatakinds. structuredname; import android. provider. contactscontract. data; import android. provider. contactscontract. raw Contacts; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. imagebutton; import android. widget. textview; import android. widget. toast; public class pinyinactivity extends activity {/** called when the activity is first created. */edittext et; button Bt; textview TV; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (save Dinstancestate); setcontentview (R. layout. main); bt = (button) findviewbyid (R. id. BT); ET = (edittext) findviewbyid (R. id. et); TV = (textview) findviewbyid (R. id. TV); BT. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {contentvalues values = new contentvalues (); Uri rawcontacturi = getcontentresolver (). insert (rawcontacts. content_uri, values); long rawcontactid = contenturi S. parseid (rawcontacturi); string name = ET. gettext (). tostring (); If (name. Length ()! = 0) {values. clear (); values. put (data. raw_contact_id, rawcontactid); values. put (data. mimetype, structuredname. content_item_type); values. put (structuredname. given_name, name); getcontentresolver (). insert (contactscontract. data. content_uri, values); hanzitopinyin (rawcontactid); Delete (rawcontactid);} else {toast. maketext (pinyinactivity. this, "enter Chinese characters! ", Toast. length_short ). show () ;}}) ;}public void hanzitopinyin (long rawcontactid) {string result = ""; string where = contactscontract. rawcontacts. contact_id + "=" + rawcontactid; string [] projection = {"sort_key"}; cursor cur = getcontentresolver (). query (contactscontract. rawcontacts. content_uri, projection, where, null, null); int pinyin1 = cur. getcolumnindex ("sort_key"); cur. movetofirst (); string pinyin = cur. getstring (pinyin1); // because of the form of Zhang zhangsan 3 obtained here, the following string is processed and changed to Zhang San for (INT I = 0; I <pinyin. length (); I ++) {string temp = pinyin. substring (I, I + 1); If (temp. matches ("[A-Za-Z]") {result = Result + temp;} else result = Result + "";} TV. settext (result. tolowercase ();} public void Delete (long rawcontactid) {getcontentresolver (). delete (contenturis. withappendedid (rawcontacts. content_uri, rawcontactid), null, null );}}

Input the Chinese character to the pinyin project and the contacts2.dbsoftware. Tutorial. Zip

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.