动态地应用主题为Android键盘键盘、动态、主题、Android

2023-09-06 07:15:24 作者:내 사랑(模仿爱情

您好人计算器,

而编程的软键盘我已经遇到了一个问题:

i have encountered a problem while programming a soft keyboard :

我想要实现的是动态软键盘加载一个主题/风格在夜间和在阳光下键入一个主题/风格输入进行了优化。该风格已经确定,我可以已经只有在input.xml中改变了风格轻松切换。

What i want to achieve is that the soft keyboard loads dynamically a theme/style optimized for typing at night and a theme/style for typing in sunlight. The styles are already defined and i can already switch them easily by only changing the style in 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;
}

当我重定向从第一构造到第二构造(如被看见在黑客的键盘源$ C ​​$ C)的号召,软键盘中成功加载loadColor()的风格ID,但之后,有内部的情况我不能管理追溯,例外,甚至与堆栈跟踪:

When i redirect the call from the first constructor to the second constructor (as seen in Hacker's Keyboard source code), the soft keyboard loads successfully the style id in loadColor(), but after it, there happens internally an exception which i can't manage to trace back, even with the stack trace :

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)

我希望你能帮助我这一点。在code动态加载的颜色是目前的INT温度动态; ,但后来我将实现逻辑(包括与其他应用我公司正在开发的键盘实际上意味着整合(光学))从共享preferences加载颜色

I hope, you can help me with that. The code for dynamically loading the color is currently as dynamic as the int temp; , but later on i'll implement the logic for loading the color from sharedpreferences (the keyboard is actually meant to integrate (optically) with another app my corp is developing)

在此先感谢您的帮助:)

Thanks in advance for any help :)

推荐答案

我发现了一个解决方案,我认为这是很简单的,只要我发现:除了黑客攻击的任何构造等,我编辑了覆盖onCreateInputView:

I found a solution, which i think is very simple as soon as i found it : Instead of hacking any constructor etc., i edited the overridden 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访问的特殊的文件夹。

Normally, the LayoutInflater would load the default input layout as intended by google. I can recommend to define multiple input layouts, each with their own style. If your own custom keyboard shall support user - made themes, just create an input layout and refer in there to a style, which refers to the theme images. Place all loaded loaded theme images in a special folder accessible to the themeable input xml.

最后一个提示:您可以通过放置android.os.Debug.waitForDebugger调试软键盘();在code。键盘(或任何其他应用程序)将等待调试器附加。记得释放你的应用程序之前删除每个了waitForDebugger()调用,否则应用程序将停止运行无处不在,你忘了删除这一行。我希望,我能帮助任何人阅读这一点。

One last hint : You can debug a soft keyboard by placing android.os.Debug.waitForDebugger(); in the code. The keyboard (or any other app) will wait for the debugger to attach. Remember to remove every waitForDebugger() call before releasing your app, or else the app will stop running everywhere you forgot to remove this line. I hope, i could help anyone reading this.

感谢您,并有一个愉快的一天:)

Thank you and have a nice day :)