Android輸入漢字得到拼音

來源:互聯網
上載者:User

前一段時間,研究Android的通訊錄,在它的contacts2.db資料庫中發現raw_contacts表中有sort_key這一列用於排序的漢語拼音等資訊,就突然想到我們可以利用它得到漢字相應的拼音.

 

不好意思,我的模擬器不能輸入中文,如果是中文名“張三”,它在這一列的存放形式應該是“ZHANG張SAN三”

那麼我的具體思路如下:

1,往表中插入漢字

2,查表,找到此欄位,經過相應的處理,輸出

3,刪除此欄位

 

contacts2.db中有好多表,想深入瞭解的,可以下載本文章結尾處的工程,裡面有本文的工程,查看contacts2.db資料庫的小軟體及相應的教程!(ps:要想匯出contacts2.db,模擬器必須先開啟)

 

這裡因為要對通訊錄進行讀寫操作,所以在AndroidManifest.xml加入相應的許可權:

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

 

閑話少說,貼代碼:

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.RawContacts;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(savedInstanceState);       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 = ContentUris.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 ,"請輸入漢字!",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);    //因為此處得到的事ZHANG張SAN三這個形式,所以下面對這個字串做處理,將它變成 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);}}

 輸入漢字得到拼音工程及查看contacts2.db軟體,教程.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.