【问题标题】:How to change color of keys in KeyboardView Class in custom Android keyboard? [duplicate]如何在自定义 Android 键盘中更改 KeyboardView 类中键的颜色? [复制]
【发布时间】:2013-08-17 07:11:25
【问题描述】:

我正在开发自定义键盘应用程序。我需要在 KeyboardView 类中为键或背景颜色设置不同的主题,并在 SoftKeyboard 扩展 InputMethodService 类的 onCreateInputView() 处获取键颜色。

但是我不知道如何根据键码获取特定键,因此我可以更改特定键的颜色或背景。

【问题讨论】:

  • 到目前为止你尝试过什么?提供您尝试过的代码将帮助您获得帮助。
  • 我已设置为 input.xml 和 input1.xml
  • 我需要在运行时获取键码,即不同的键颜色我使用了三个 input.xml,input1.xml,input2.xml 如果 input.xml 膨胀,则 keycolor 将是绿色,如果 input1 则为蓝色组合.xml 膨胀键背景颜色为红色和橙色 .. 与 input2.xml 类似,所以我不知道该怎么做

标签: android android-softkeyboard


【解决方案1】:

在任何不同的输入布局上使用 android:keyBackground=".."

示例:

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"
            />

然后在 OnCreateInputView 方法上:

@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;
}

并在方法 onStartInput 的末尾添加:

setInputView(onCreateInputView());

如果您已经这样做了,并且您需要为特殊键设置不同的背景。也许我写的问题的解决方案会对你有所帮助:https://stackoverflow.com/a/18354298/2683898

祝你好运! :)

【讨论】:

  • 另外,背景不一定是可绘制的。它也可以是颜色资源。
  • 此代码用于更改键盘背景的颜色而不是键盘文本颜色。想要以编程方式更改颜色
猜你喜欢
  • 1970-01-01
  • 2019-08-25
  • 2016-12-03
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 2017-11-04
  • 2019-05-26
相关资源
最近更新 更多