在运行时间上softkeyboard更改按键图标
问题描述:
我试图改变一个钥匙图标上按上softkeyboard在运行时间:在运行时间上softkeyboard更改按键图标
@Override
public void onPress(int primaryCode) {
Keyboard currentKeyboard = mInputView.getKeyboard();
List<Keyboard.Key> keys = currentKeyboard.getKeys();
mInputView.invalidateKey(primaryCode);
keys.get(primaryCode).label = null;
keys.get(primaryCode).icon = ContextCompat.getDrawable(getApplicationContext(), android.R.drawable.ic_dialog_email);
}
它的工作原理,但是当我按一个键,改变其他键的图标。你知道为什么吗?
(我使用的API级别8)
答
做这个
@Override
public void onPress(int primaryCode)
{
Keyboard currentKeyboard = mInputView.getKeyboard();
List<Keyboard.Key> keys = currentKeyboard.getKeys();
mInputView.invalidateKey(primaryCode);
for(int i = 0; i < keys.size() - 1; i++)
{
Keyboard.Key currentKey = keys.get(i);
//If your Key contains more than one code, then you will have to check if the codes array contains the primary code
if(currentKey.codes[0] == primaryCode)
{
currentKey.label = null;
currentKey.icon = getResources().getDrawable(android.R.drawable.ic_dialog_email);
break; // leave the loop once you find your match
}
}
}
原因,你的代码是不工作:这里的罪魁祸首是keys.get(primaryCode)
。您需要从Keys列表中获取Key
。因为方法List
需要您想要获取的对象的位置。但是你并没有传递它们对象的位置,而不是传递关键字的unicode值。所以,我所做的只是使用它的位置从List
中正确获取Key
。现在,我通过运行for
循环来获得位置,并将每个Key
的unicode值与当前按下的Key
的unicode值进行比较。
注:在某些情况下,一个Key
有一个以上的Unicode值。在这种情况下,您将不得不更改if
语句,您将在其中检查代码数组是否包含。
这是什么意思**它可以工作,但是当我按下某个键时,请更改其他键的图标** – Rohit5k2
例如,如果我点击“a”键,“f”键图标会更改并且“a”键不会改变。但我想更改“a”键图标... – highpass
我从来没有这样做过,所以我不得不编写自定义键盘代码来查看这里发生了什么,最后我发现代码中出了什么问题。请看我的答案。 – Rohit5k2