软键盘上的自定义颜色

custom colors on softkeyboard

我有这个键盘:

我认为键盘来自基本键盘教程。

我想改变每个按键的背景:

这里我放了每一个键:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"  
android:keyHeight="@dimen/key_height">

<Row>
    <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
    <Key android:codes="119" android:keyLabel="w"/>
    <Key android:codes="101" android:keyLabel="e"/>
    <Key android:codes="114" android:keyLabel="r"/>
    <Key android:codes="116" android:keyLabel="t"/>
    <Key android:codes="121" android:keyLabel="y"/>
    <Key android:codes="117" android:keyLabel="u"/>
    <Key android:codes="105" android:keyLabel="i"/>
    <Key android:codes="111" android:keyLabel="o"/>
    <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
</Row>

我不知道我是否可以从那里更改样式

更新 我的键盘视图:

<android.inputmethodservice.KeyboardView
        android:id="@+id/keyboardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:focusableInTouchMode="true"
        android:keyBackground="@drawable/message_bg"
        android:keyPreviewLayout="@layout/preview">
</android.inputmethodservice.KeyboardView>

Keyboard.java

    public void onInitializeInterface() {

    qwertyKeyboard = new Keyboard(this, R.xml.qwerty);

    symbolsKeyboard = new Keyboard(this, R.xml.symbols);

    symbolsShiftedKeyboard = new Keyboard(this, R.xml.symbols_shift);

    fillKeyboard = (LinearLayout) getLayoutInflater().inflate(R.layout.fill_keyboard, null);

    dialogAccounts = new Builder(this);

    dialogAllAccounts = new Builder(this);

    usersTextView = (Button) fillKeyboard.findViewById(R.id.userTextView);

    noCredentials = (Button) fillKeyboard.findViewById(R.id.noCredentials);

} 

/**
 * Select the view in the keyboard depending of the Globals.isLoged
 */
@Override
public View onCreateInputView() {

    Globals.isKeyboard = true;

    Globals.tokenKeyboard = this.getWindow().getWindow().getAttributes().token;

    database = new PwdDatabaseHelper(this);
    loginKeyboard = (LinearLayout) getLayoutInflater().inflate(R.layout.log_keyboard, null);

    if (!Globals.isLoged) {

        myMasterPassword = (EditText) loginKeyboard.findViewById(R.id.masterPasswordKeyboard);

        currentKeyboardView = (KeyboardView) loginKeyboard.findViewById(R.id.keyboardView);

        currentKeyboardView.setKeyboard(qwertyKeyboard);

        currentKeyboardView.setOnKeyboardActionListener(this);

        return loginKeyboard;
    } else {

        if (fillUsersAndPass() != 0) {
            currentKeyboardView = (KeyboardView) fillKeyboard.findViewById(R.id.keyboardView);
            currentKeyboardView.setKeyboard(qwertyKeyboard);
            currentKeyboardView.setOnKeyboardActionListener(this);
            noCredentials.setVisibility(View.GONE);

            usersTextView.setVisibility(View.VISIBLE);

            return fillKeyboard;
        } else {

            currentKeyboardView = (KeyboardView) fillKeyboard.findViewById(R.id.keyboardView);
            noCredentials.setVisibility(View.VISIBLE);
            usersTextView.setVisibility(View.GONE);
            currentKeyboardView.setKeyboard(qwertyKeyboard);
            currentKeyboardView.setOnKeyboardActionListener(this);
            return fillKeyboard;
        }

    }
}

更新 2

keyboard.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
      
<item android:state_pressed="true"
      android:color="#8e44ad"/> <!-- pressed -->
<item android:state_focused="true"
      android:color="#e67e22"/> <!-- focused -->
<item android:color="#e74c3c"/>  <!--default -->

</shape>

您可以从 KeyboardView 更改按键背景。在您的 xml 中,将此添加到您的 KeyboardView:

android:keyBackground="@drawable/yourDrawable"
android:keyPreviewLayout="@layout/yourLayout"

其中yourDrawable是可绘制文件(不能是图片),yourLayout是父视图设置为TextView的布局。

更多信息:http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html

编辑:

正如 google 文档所说:

android:keyBackground

Image for the key. This image needs to be a StateListDrawable, with the following possible states: normal, pressed, checkable, checkable+pressed, checkable+checked, checkable+checked+pressed.

因此您必须创建可绘制的形状文件而不是图像。

示例:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_pressed="true"
        android:color="#8e44ad" /> <!-- pressed -->

    <item
        android:state_focused="true"
        android:color="#e67e22" /> <!-- focused -->

    <item
        android:color="#e74c3c" /> <!-- default -->

</selector>

参考:http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html#attr_android:keyBackground