How to change color of keys in KeyboardView Class in custom Android keyboard? [duplicate]

六眼飞鱼酱① 提交于 2019-12-18 17:25:34

问题


I am working on a Custom Keyboard App. I need to set different themes for keys or background color in KeyboardView class and get key color at onCreateInputView() in SoftKeyboard extends InputMethodService Class.

However I am not getting how to get particular key according to keycode so I can change color or background of particular key.


回答1:


On any diffrent input layout use android:keyBackground=".."

example:

input.xml:

<com.example.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:keyBackground="@drawable/blue_key"
        />

input1.xml:

<com.example.KeyboardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/keyboard"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:keyBackground="@drawable/red_key"
            />

then on OnCreateInputView method:

@Override public View onCreateInputView() {
    if(theme == 1)
        mInputView = (KeyboardView) getLayoutInflater().inflate(R.xml.input , null);
    else
        mInputView = (KeyboardView) getLayoutInflater().inflate(R.xml.input1 , null);
    mInputView.setOnKeyboardActionListener( this);
    mInputView.setKeyboard(mQwertyKeyboard);
    mComposing.setLength(0);
    return mInputView;
}

and on the end of the method onStartInput add this:

setInputView(onCreateInputView());

If you've already done it and what you need is to set a different background to special keys. Maybe the solution to my problem that I wrote will help you: https://stackoverflow.com/a/18354298/2683898

Good Luck! :)



来源:https://stackoverflow.com/questions/18286164/how-to-change-color-of-keys-in-keyboardview-class-in-custom-android-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!