Android keyboard Key background

How to set backgrounds for each key on an Android keyboard. KeyboardView android: keyBackground provides one background for all keys. But I want to set different backgrounds for each key.

+7
android keyboard
source share
4 answers

If you are writing your own IME, try using drawable for a FOREground image (with android: keyIcon in XML or Key.icon in Java), which is equal to the size of the entire key. This is essentially equivalent to setting the background image of a single key. Of course, you will also have to include the foreground in your image.

You will also have to use a background image without indentation, so it does not look around the edges. For more information on how to do this, see this post: How ppppppppppp Works in Android Applications.

Barry

+5
source share

I customize MyKeyBoradView by extending KeyBoardView and overriding the onDraw method.

public class MyKeyBoardView extends KeyboardView { private Context mContext; private Keyboard mKeyBoard; public MyKeyBoardView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; } public MyKeyBoardView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext = context; } /** * ov */ @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); mKeyBoard = this.getKeyboard(); List<Key> keys = null; if (mKeyBoard != null) { keys = mKeyBoard.getKeys(); } if (keys != null) { for (Key key : keys) { // TODO: 16/8/23 different key set the different background if (key.codes[0] == -4) { drawKeyBackground(R.drawable.bg_keyboardview_yes, canvas, key); drawText(canvas, key); } } } } private void drawKeyBackground(int drawableId, Canvas canvas, Key key) { Drawable npd = mContext.getResources().getDrawable( drawableId); int[] drawableState = key.getCurrentDrawableState(); if (key.codes[0] != 0) { npd.setState(drawableState); } npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); npd.draw(canvas); } private void drawText(Canvas canvas, Key key) { Rect bounds = new Rect(); Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); paint.setAntiAlias(true); paint.setColor(Color.WHITE); if (key.label != null) { String label = key.label.toString(); Field field; if (label.length() > 1 && key.codes.length < 2) { int labelTextSize = 0; try { field = KeyboardView.class.getDeclaredField("mLabelTextSize"); field.setAccessible(true); labelTextSize = (int) field.get(this); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } paint.setTextSize(labelTextSize); paint.setTypeface(Typeface.DEFAULT_BOLD); } else { int keyTextSize = 0; try { field = KeyboardView.class.getDeclaredField("mLabelTextSize"); field.setAccessible(true); keyTextSize = (int) field.get(this); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } paint.setTextSize(keyTextSize); paint.setTypeface(Typeface.DEFAULT); } paint.getTextBounds(key.label.toString(), 0, key.label.toString() .length(), bounds); canvas.drawText(key.label.toString(), key.x + (key.width / 2), (key.y + key.height / 2) + bounds.height() / 2, paint); } else if (key.icon != null) { key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2, key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight()); key.icon.draw(canvas); } } 

}

The implementation effect is as follows enter image description here

Links: https://github.com/xuejinwei/NumberKeyboard

+4
source share

I also tried to do this. The main background is drawn in onBufferDraw () in the KeyboardView class. The problem is that this is a private method, so you cannot override it with a subclass. So I tried to completely copy the KeyboardView and change it, but it uses the resources com.android.internal.R , which the external application does not have access to. So this approach does not work.

At this moment, it starts to look like I have to throw Android keyboard classes out of the window and write everything from scratch - all b / c. I cannot change the background image of a space. Funny.

+2
source share

Perhaps you could define a new Input Method .

0
source share

All Articles