keyBackground特殊键 - 安卓键盘

时间:2013-08-14 22:22:44

标签: android xml android-layout android-softkeyboard

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

这是我的slector代码:

<?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>

1 个答案:

答案 0 :(得分:10)

<强>解决方案:

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

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

现在,如何编码: 这是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之外,我还添加了STATE_FUNCTION和STATE_FUNCTION_PRESSED。 然后我重写了getCurrentDrawableState方法并向该方法添加了新状态。之后,如果键是修饰符,它将是STATE_FUNCTION并使用drawableStateList中的state_single。

现在在LatinKeyboard上:

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类并享受:)