为Android键盘动态应用主题

时间:2015-03-19 07:44:28

标签: java android keyboard styles ime

StackOverflow的人们,

编程软键盘时遇到问题:

我想要实现的是软键盘动态加载优化的主题/风格,用于在夜间打字,以及用于在阳光下打字的主题/风格。样式已经定义,只需更改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;
}

当我将调用从第一个构造函数重定向到第二个构造函数时(如Hacker的Keyboard源代码中所示),软键盘成功加载了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一样动态; ,但稍后我将实现从共享偏好加载颜色的逻辑(键盘实际上是为了与我公司正在开发的另一个应用程序集成(光学))

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分: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()调用,否则应用程序将在您忘记删除此行的任何地方停止运行。我希望,我可以帮助任何人阅读这篇文章。

谢谢你,祝你有愉快的一天:)