2016-03-02 18 views
0

我正在用java編寫英文/阿拉伯語字典。我希望應用根據搜索語言自動將鍵盤設置爲合適的佈局。我保存的鍵盤區域設置信息是這樣的:使用InputContext.selectInputMethod指定鍵盤佈局總是失敗

JTextField tf = (JTextField) e.getSource(); 
    InputContext ic = tf.getInputContext(); 
    Pref.setInputLocale (Pref.getSearchIndex(), ic.getLocale().toString()); 

而且這樣的方式進行恢復:

JTextField tf = Ui.makeTextField (20, "", searchListener); 
    controlPanel.add(tf); 
    String ls = Pref.getInputLocale(Pref.getSearchIndex()); 
    if (ls.length() > 0) { 
     Locale l = new Locale (ls); 
     InputContext ic = tf.getInputContext(); 
     System.out.println (ic.toString()); 
     System.out.println (l.toString()); 
     System.out.println (ic.selectInputMethod (l)); 
    } 

系統輸出輸入語言的三個連續交換機看起來是這樣的:

[email protected] 
en_us 
false 
[email protected] 
ar_eg 
false 
[email protected] 
en_us 
false 

系統輸出顯示語言環境字符串正在保存併成功恢復,但InputContext.selectInputMethod始終返回false,並且鍵盤不轉換編輯阿拉伯文。有什麼建議麼?

回答

0

問題出現是因爲ic.getLocale.toString()產生「en_us」,而Locale()想要「en」,「US」。

的解決方案是,使用此代碼替換此代碼

 Locale l = new Locale (ls); 

 ls = ls.replace("_", "-"); 
     Locale l = new Locale.Builder().setLanguageTag(ls).build(); 

這首先,接通下劃線爲負,然後使用setLanguageTag這 容忍,爲國家小寫字母。