2015-03-19 51 views
1

你好的人在StackOverflow上,應用主題動態的Android鍵盤

我遇到了一個問題,同時編程軟鍵盤:

我想要實現的是軟鍵盤動態加載一個主題/風格的優化在夜間打字和在陽光下打字的主題/風格。這些樣式已經定義好了,我可以通過改變input.xml中的樣式來輕鬆切換它們。

import ... /*Not quite relevant (yet)*/ 

public class LatinKeyboardView extends KeyboardView { 

public LatinKeyboardView(Context context, AttributeSet attrs) { 
    this(context, attrs, loadColor()); 
} 

public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle)     { 
    super(context, attrs, defStyle); 

} 

public static int loadColor() { 

    String dbg = android.os.Debug.isDebuggerConnected() ? "Y" : "N"; 

    android.util.Log.d("loadColor", "Debugger connected: " + dbg); 

    android.os.Debug.waitForDebugger(); 

    int temp = 0; 
    int id = 0; 

    if (temp == 1) 
    { 
     id = R.style.KeyStyle_Day; 
    } 
    else 
    { 
     id = R.style.KeyStyle_Night; 
    } 

    android.util.Log.d("loadColor", Integer.toString(id)); 
    return id; 
} 

當我重定向從第一構造到第二構造(如被看見在黑客的鍵盤源代碼)的呼叫時,軟鍵盤中成功加載loadColor()的風格ID,但之後呢,有內部發生一個例外,我無法追溯,即使堆棧跟蹤:

03-19 08:32:48.313 18630-18630/com.alphanerd.eztype.keyboard E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.alphanerd.eztype.keyboard, PID: 18630 
    android.view.InflateException: Binary XML file line #21: Error inflating class com.alphanerd.eztype.keyboard.LatinKeyboardView 
      at android.view.LayoutInflater.createView(LayoutInflater.java:626) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:470) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
      at com.alphanerd.eztype.keyboard.EZTypeKeyboard.onCreateInputView(EZTypeKeyboard.java:125) 
      at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1594) 
      at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:2220) 
      at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:2179) 
      at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:712) 
      at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:214) 
      at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:157) 
      at android.app.ActivityThread.main(ActivityThread.java:5335) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Constructor.constructNative(Native Method) 
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
      at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:470) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
            at com.alphanerd.eztype.keyboard.EZTypeKeyboard.onCreateInputView(EZTypeKeyboard.java:125) 
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1594) 
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:2220) 
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:2179) 
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:712) 
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:214) 
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:157) 
            at android.app.ActivityThread.main(ActivityThread.java:5335) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at android.inputmethodservice.KeyboardView.<init>(KeyboardView.java:370) 
      at com.alphanerd.eztype.keyboard.LatinKeyboardView.<init>(LatinKeyboardView.java:53) 
            at java.lang.reflect.Constructor.constructNative(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
            at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:470) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
            at com.alphanerd.eztype.keyboard.EZTypeKeyboard.onCreateInputView(EZTypeKeyboard.java:125) 
            at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1594) 
            at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:2220) 
            at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:2179) 
            at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:712) 
            at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:214) 
            at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:157) 
            at android.app.ActivityThread.main(ActivityThread.java:5335) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
            at dalvik.system.NativeStart.main(Native Method) 

我希望,你可以幫助我。動態加載顏色的代碼當前與int temp一樣動態; ,但後來我將實現邏輯從sharedpreferences(包括與其他應用我公司正在開發的鍵盤實際上意味着集成(光學))預先加載的顏色任何幫助:)

回答

1

謝謝

我發現了一個解決方案,我認爲這是很簡單的,只要我發現:除了黑客的任何構造等,我編輯了重寫onCreateInputView:

@Override 
public View onCreateInputView() { 
    int schema = loadColor(); 

    //Default code 

    //mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
    //  R.layout.input, null); 

    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
      schema, null); 

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

public static int loadColor() { 

    String dbg = android.os.Debug.isDebuggerConnected() ? "Y" : "N"; 

    android.util.Log.d("loadColor", "Debugger attached: " + dbg); 

    //android.os.Debug.waitForDebugger(); //Comment this out before releasing the keyboard 

    int temp = 1; 
    int id = 0; 

    if (temp == 1) 
    { 
     id = R.layout.input_day; 
    } 
    else 
    { 
     id = R.layout.input; 
    } 

    android.util.Log.d("loadColor", Integer.toString(id)); 
    return id; 
} 

通常情況下,LayoutInflater將加載默認的輸入佈局正如谷歌打算的那樣。我可以推薦定義多個輸入佈局,每個佈局都有自己的風格。如果您自己的自定義鍵盤應支持用戶製作的主題,則只需創建一個輸入佈局並在其中引用指向主題圖像的樣式。將所有已加載的加載主題圖像放置在可訪問主題輸入xml的特殊文件夾中。

最後一個提示:您可以通過放置android.os.Debug.waitForDebugger()來調試軟鍵盤;在代碼中。鍵盤(或任何其他應用程序)將等待調試器附加。請記住,在釋放您的應用程序之前刪除每個waitForDebugger()調用,否則應用程序將在您忘記刪除此行的任何地方停止運行。我希望,我可以幫助任何人閱讀。

謝謝,祝你有美好的一天:)