gpt4 book ai didi

安卓键盘。 key 的背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:20 26 4
gpt4 key购买 nike

如何为 android 键盘上的每个键设置背景。KeyboardView android:keyBackground 为所有按键提供一个背景。但是我想为每个键设置不同的背景。

最佳答案

我自定义 MyKeyBoradView 扩展 KeyBoardView 并覆盖 onDraw 方法。

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 = spToPx((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 = spToPx((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);
}

}
public int spToPx(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getContext().getResources().getDisplayMetrics());
}
}

实现效果如下 enter image description here

此链接:https://github.com/xuejinwei/NumberKeyboard

关于安卓键盘。 key 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3185237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com