How to set Different Key Background for every Key in Android Custom Keyboard
我正在使用自定义键盘,我需要在运行时或动态地为每个键设置不同的图像,但我不知道如何做到这一点。
我正在使用这个代码:
1 2 3 4 5 6 7 8 9 10 11
| <com.keyboard.KeyBoard.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.keyboard.KeyBoard"
android:id="@+id/keyboard"
android:keyBackground="@drawable/black_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:background="@android:color/black"
android:focusableInTouchMode="true" /> |
现在我想在
中获取 android:keyBackground="@drawable/black_bg" 属性
1 2 3 4
| @Override
public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
} |
因为我必须在此处动态更改键的图像背景,但我不知道如何做到这一点。
- 我认为可以在这里找到类似的答案-> stackoverflow.com/questions/6000543/
-
但是我想设置键的背景而不是文本我不知道该怎么做请帮助我
-
这里的"键"是 for 循环:for(Key key: keys) - 这个东西会遍历键盘的所有键,你可以设置键图标(参见这个 developer.android.com/reference/android/inputmethodservice/ ),其中取自 developer.android.com/reference/android/inputmethodservice/ 。我认为这是单独更改密钥的唯一方法。您的方法将更改所有按钮的背景。
-
对于键,这是不可能的,因为键没有恒定的背景..但是您可以将可绘制图像设置为键的背景..为此,您必须创建不同颜色的可绘制对象
-
@kalyanpvs我如何设置drawable实际上我只需要设置一个图像,即。白色图像第一次用户将选择黑色而不是所有键在背景中都有黑色图像你可以告诉我
-
键盘键盘 = keyboardView.getKeyboard(); \\t\\tList<Key> 键 = 键盘.getKeys(); \\t\\tHashMap<String, int[]> data = new HashMap<String, int[]>(); \\t\\tfor (Key key : keys) { \\t\\t\\tif (Character.isLetter(key.codes[0])) { \\t\\t\\t\\tkey.icon=你的可绘制图像; \\t\\t\\t} \\t\\t}
-
@Kalyanpvs 你的意思是 if (Character.isLetter(key.codes[0])) \\t{ \\t\\tkey.icon= R.drawable.whitebg; \\t} 但我在 key.icon= R.drawable.whitebg 上遇到错误;行请告诉我
-
实际上 android 框架并没有为我们提供在运行时更改关键背景的能力。如果您想实现这一点(例如更改皮肤),您必须使用自定义视图完成大部分绘图工作。
-
看看我的回答 http://stackoverflow.com/a/39091883/4964307 希望对你有所帮助。
后来有一个重复的问题发布在
如何更改Android软键盘中任意键的键背景。它有一些很好的答案。