2013-01-07 138 views
3

我正在創建自定義鍵盤。我從Here得到了這樣一個好的和有用的演示。我想創建鍵盤的多個主題,所以我爲鍵盤創建另一個佈局,但現在的問題是我不知道如何設置當前鍵盤的佈局,或者必須新加載鍵盤或必須執行其他操作。鍵盤的設計沒有任何想法。動態更改鍵盤佈局

我的理念是用戶必須從活動中選擇鍵盤主題,並且鍵盤設計會發生變化。

任何人都可以幫助我或有任何想法推出這個問題..?

回答

10

獲取解決方案以更改自定義鍵盤的佈局。

當鍵盤第一次加載onCreateInputView()時被調用。之後,當鍵盤打開onStartInputView(EditorInfo屬性,布爾重新啓動)每次調用。

所以,現在鍵盤(主題)的佈局有onCreateInputView()這樣定義

public KeyboardView mInputView; 
public View onCreateInputView() { 

    SharedPreferences pre = getSharedPreferences("test", 1); 
    int theme = pre.getInt("theme", 1); 

    if(theme == 1) 
    { 
     this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input, null); 
    }else 
    { 
     this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input_2, null); 

    } 
    this.mInputView.setOnKeyboardActionListener(this); 
    this.mInputView.setKeyboard(this.mQwertyKeyboard); 
    return this.mInputView; 
} 

和onStartInputView

public void onStartInputView(EditorInfo attribute, boolean restarting) { 
    super.onStartInputView(attribute, restarting); 

    setInputView(onCreateInputView()); 
} 
+0

我如何處理點擊我的自定義佈局的事件做到這一點。 – Kaustubh

+0

謝謝@Yog Guru :) –

+0

這種重新創建整個輸入視圖的方法特別好,因爲KeyboardView錯誤地緩存了keyWidth,使得用簡單的setKeyboard進行切換佈局成爲不可能。 –