android自定义键盘画一些键

时间:2014-11-21 17:17:41

标签: java android

我正在Android中开始一些教程,而我正在尝试制作自定义键盘 它看起来不错,但我想画一些键。所以我将课程扩展到KeyboardView并实现onDraw()功能。当我调试键盘时,它永远不会进入我的onDraw功能。以下是我的代码的一些部分。

public class CustomKeyboard extends KeyboardView {

    private KeyboardView mKeyboardView;

    public CustomKeyboard(Activity host,  AttributeSet attrs, int viewid, int layoutid) {
        super(host, attrs);

        mHostActivity= host;
        mKeyboardView= (KeyboardView)mHostActivity.findViewById(viewid);
        mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
        mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(25);
        paint.setColor(Color.RED);

        List<Key> keys = getKeyboard().getKeys();
        for(Key key: keys) {
            if(key.label != null)
            canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
        }
    }
}

0 个答案:

没有答案