how to change key background of any key in Android soft keyboard

后端 未结 3 485
暖寄归人
暖寄归人 2021-02-05 16:52

I want to have some of my keys in the keyboard different from others.

For example,like shift, delete, space key in below photo:

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 17:19

    The first thing to note is that there is no simple way to do this in XML, you will need to do it in Java.

    You can get the list of keys using the following piece of code:

    Keyboard keyboard = keyboardView.getKeyboard();
    List keys = keyboard.getKeys();
    

    Once you have the list of keys, you can iterate through them to find the ones you want to change, using a for loop like:

    for (Key key : keys) {
        ...
    }
    

    Please refer to the documentation here for the properties of Keyboard.Key in Android. You'll probably want to use the codes to distinguish the keys.

    You will notice that there is no direct way of changing the background color of a key. However, there is a way to change the icon, so you can use that to simulate the same behaviour. You could generate drawable objects that contain the color you need and set it as the icon. You could use a NinePatchDrawable.

    You can add this functionality either in the onStartInputView() method or in the onDraw() method. You can see a piece of code that is supposedly working here.

    Alternatively, you could decide to implement your own keyboard. In case you want to do that, you can see the Android implementation here. You could copy this and modify it directly to suit your needs.

提交回复
热议问题