- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我基于软键盘开发Android键盘。现在设计选择器中使用的键盘并更改键背景现在一切正常,除了单个键和 Activity 键,它们显示为标准键:(
这是我的选择器代码:
<?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>
最佳答案
解决方案:
经过多次尝试,我找到了解决问题的方法!在原始的 Keyboard 类中,Single/Active Drawable State 是不确定的。所以我们需要做的就是重写这个类。
怎么做?如果您的键盘基于 SoftKeyboard,我们有一个扩展 Keyboard 的 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 类并享受 :)
关于android - 特殊键的keyBackground - android键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243323/
我正在尝试为我的 KeyboardView 设置自定义背景。设置 XML 属性 keyBackground似乎是解决它的方法,但是当它试图膨胀可绘制对象时,我的 logcat 中不断出现 NullPo
我是一名优秀的程序员,十分优秀!