4

我基于软键盘开发安卓键盘。现在设计选择器中使用的键盘并更改 keybackgroung 现在一切正常,除了单个键和活动它们显示为标准键:(

这是我的选择器代码:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_single="true" android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_dark_pressed_holo" />
    <item android:state_single="true" android:drawable="@drawable/btn_keyboard_key_dark_normal_holo" />
    <item android:state_active="true" android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_dark_pressed_holo" />
    <item android:state_active="true" android:drawable="@drawable/btn_keyboard_key_dark_active_holo" />
    <item android:state_checkable="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_dark_pressed_on_holo" />
    <item android:state_checkable="true" android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_dark_pressed_off_holo" />
    <item android:state_checkable="true" android:state_checked="true" android:drawable="@drawable/btn_keyboard_key_dark_normal_on_holo" />
    <item android:state_checkable="true" android:drawable="@drawable/btn_keyboard_key_dark_normal_off_holo" />
    <item android:state_pressed="true" android:drawable="@drawable/btn_keyboard_key_light_pressed_holo" />
    <item android:drawable="@drawable/btn_keyboard_key_light_normal_holo" />
</selector>
4

1 回答 1

10

解决方案:

经过多次尝试,我找到了解决问题的方法!在原始键盘类中,单一/活动可绘制状态是不确定的。所以我们需要做的是重写这个类。

怎么做?如果您的键盘基于 SoftKeyboard,我们有一个扩展键盘的 LatinKeyboard 类。如果您没有此类,请创建它!现在在这个类上,我们有一个扩展 Keyboard.Key 的 LatinKey 静态类。

现在,如何编码:这是 LatinKey 类代码:

static class LatinKey extends Key {

        public LatinKey(Resources res, Row parent, int x, int y, XmlResourceParser parser) {
            super(res, parent, x, y, parser);
        }

        private final static int[] KEY_STATE_NORMAL_ON = { 
            android.R.attr.state_checkable, 
            android.R.attr.state_checked
        };

        private final static int[] KEY_STATE_PRESSED_ON = { 
            android.R.attr.state_pressed, 
            android.R.attr.state_checkable, 
            android.R.attr.state_checked 
        };

        private final static int[] KEY_STATE_NORMAL_OFF = { 
            android.R.attr.state_checkable 
        };

        private final static int[] KEY_STATE_PRESSED_OFF = { 
            android.R.attr.state_pressed, 
            android.R.attr.state_checkable 
        };

        private final static int[] KEY_STATE_FUNCTION = { 
            android.R.attr.state_single
        };

        private final static int[] KEY_STATE_FUNCTION_PRESSED = { 
            android.R.attr.state_pressed, 
            android.R.attr.state_single
        };

        private final static int[] KEY_STATE_NORMAL = {
        };

        private final static int[] KEY_STATE_PRESSED = {
            android.R.attr.state_pressed
        };

        @Override
        public int[] getCurrentDrawableState() {
            int[] states = KEY_STATE_NORMAL;

            if (on) {
                if (pressed) {
                    states = KEY_STATE_PRESSED_ON;
                } else {
                    states = KEY_STATE_NORMAL_ON;
                }
            } else {
                if (sticky) {
                    if (pressed) {
                        states = KEY_STATE_PRESSED_OFF;
                    } else {
                        states = KEY_STATE_NORMAL_OFF;
                    }
                } else if(modifier){
                    if (pressed) {
                        states = KEY_STATE_FUNCTION_PRESSED;
                    } else {
                        states = KEY_STATE_FUNCTION;
                    }
                } else {
                    if (pressed) {
                        states = KEY_STATE_PRESSED;
                    }
                }
            }
            return states;
        }
    }

除了另一个原始状态之外,我还添加了 STATE_FUNCTION 和 STATE_FUNCTION_PRESSED。然后我重写了 getCurrentDrawableState 方法并向该方法添加了新状态。之后,如果键是修饰符,它将是 STATE_FUNCTION 并使用 drawableStateList 中的 state_single。

现在在拉丁键盘上:

public class LatinKeyboard extends Keyboard {

        public LatinKeyboard(Context context, int xmlLayoutResId) {
            super(context, xmlLayoutResId);
            // TODO Auto-generated constructor stub
        }

        public LatinKeyboard(Context context, int layoutTemplateResId, 
                CharSequence characters, int columns, int horizontalPadding) {
            super(context, layoutTemplateResId, characters, columns, horizontalPadding);
        }

        @Override
        protected Key createKeyFromXml(Resources res, Row parent, int x, int y, 
                XmlResourceParser parser) {
            Key key = new LatinKey(res, parent, x, y, parser);
            return key;
        }
   static class LatinKey extends Keyboard.Key {
     //LatinKey code...
   }
}

我们覆盖了 createKeyFromXml 并返回了新的 LatinKey,现在在主键盘类上使用新的 LatinKeyboard 类并享受:)

于 2013-08-21T09:55:22.773 回答