1、WPF的一個觸摸屏項目,需要輸入姓名,但是屏幕不支持漢字輸入,使用虛擬鍵盤不穩定,為了解決該問題特此進行處理。
2、新建一個類轉換類,裡面初始化一個數組,數組包含拼音,以及拼音下的常用的漢字。
3、根據拼音,對照出對應的漢字,然後在對漢字進行拆分,獲取到單個漢字。
例子如下:
private static readonly string[][] _Allhz =
new string[][]
{
new string[]{"A","啊阿呵吖嗄腌锕錒"},
new string[]{"Ai","愛矮挨哎礙癌艾唉哀藹隘埃皚呆嗌嫒瑷暧捱砹"},
new string[]{"An","按安暗岸俺案鞍氨胺廠廣庵揞犴铵桉谙鹌埯黯"},
new string[]{"Ang","昂骯盎仰卬岇昻枊醃醠骯"},
new string[]{"Ao","襖凹傲奧熬懊敖翱澳囂拗媪廒骜嗷坳遨聱螯獒鏊鳌鏖"},
new string[]{"Ba","把八吧爸拔罷跋巴芭扒壩霸叭靶笆疤耙捌粑茇岜鲅钯魃菝灞"},
new string[]{"Bai","百白擺敗柏拜佰伯稗捭呗掰"},
~~~~~~~~//省去若干
new string[]{"Zui","最嘴醉罪堆咀觜蕞"},
new string[]{"Zun","尊遵鳟撙樽僎僔噂墫奠嶟拵捘捽栫瀳繜罇袸譐跧踆蹲銌鐏鱒鶎鷷"},
new string[]{"Zuo","做作坐左座昨鑿琢撮佐笮"}
};
/// <summary>
/// 拼音轉漢字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ConvertPzToHz(string str)
{
str = str.ToLower();
if (str == null)
return null;
byte[] bAry = new byte[2];
StringBuilder rtnSb = new StringBuilder();
for (int j = 0; j < _Allhz.Length; j++)
{
string py = _Allhz[j][0].ToLower();
if (py.IndexOf(str) != -1)
{
rtnSb.Append(_Allhz[j][1]);
break;
}
}
return rtnSb.ToString();
}
//對漢字字符串進行分解
char[] chars = strMsg.ToCharArray();
ASP方法
<script language="vbs" src="spellcode.vbs"> </script>
'獲取客戶簡稱的拼音縮寫
<%
function getpy()
dim str
dim i
str=document.all.txtContactName.value
dim ret
for i=1 to len(str)
ret=ret&getpychar(mid(str,i,1))
next
document.all.txtSpellCode.value=ret
end function
getpychar=char
end if
end function
spellCode.vbs
'獲取漢字拼音第一字母
function getpychar(char)
dim tmp
tmp=65536+asc(char)
if(tmp> =45217 and tmp <=45252) then
getpychar= "A"
elseif(tmp> =45253 and tmp <=45760) then
getpychar= "B"
elseif(tmp> =45761 and tmp <=46317) then
getpychar= "C"
elseif(tmp> =46318 and tmp <=46825) then
getpychar= "D"
elseif(tmp> =46826 and tmp <=47009) then
getpychar= "E"
elseif(tmp> =47010 and tmp <=47296) then
getpychar= "F"
elseif(tmp> =47297 and tmp <=47613) then
getpychar= "G"
elseif(tmp> =47614 and tmp <=48118) then
getpychar= "H"
elseif(tmp> =48119 and tmp <=49061) then
getpychar= "J"
elseif(tmp> =49062 and tmp <=49323) then
getpychar= "K"
elseif(tmp> =49324 and tmp <=49895) then
getpychar= "L"
elseif(tmp> =49896 and tmp <=50370) then
getpychar= "M"
elseif(tmp> =50371 and tmp <=50613) then
getpychar= "N"
elseif(tmp> =50614 and tmp <=50621) then
getpychar= "O"
elseif(tmp> =50622 and tmp <......余下全文>>
有一個工具叫做pinyin4j,就是把漢字轉換成對應的拼音的工具,你只需要把數據庫中的關鍵詞轉換成漢語拼音,然後在輸入漢語拼音的時候去匹配這些,就可以了。