【问题标题】:Android Custom KeyBoardView clear default keysAndroid Custom KeyBoardView 清除默认键
【发布时间】:2013-07-01 19:08:08
【问题描述】:

我有自定义 MyKBV 类,它扩展了 KeyBoardView。我创建了这个自定义视图来为按键使用自定义字体。我能够在键上看到更改后的字体,但问题是每个键与 XML 中的默认键重叠,我认为这是 TypefaceE.DEFAULT_BOLD.SO 我看到的是每个键上有两个字符串,一个粗体,一个带有我想要的字体。如何清除默认键,以便只有自定义键可见。我在这方面花了很多时间。如果有人能指出我哪里出错或我能做什么,那将很有帮助做。谢谢!!

public class MyKBV extends KeyboardView {
Context context;

@Override
public void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    Paint paint = new Paint();
    Typeface font = Typeface.createFromAsset(context.getAssets(),
            "fonts/Hippie.otf");
    paint.setTypeface(font);
    paint.setTextSize(40);

    List<Key> listKeys = getKeyboard().getKeys();

    for (Key key : listKeys) {
        if (key.label != null) {
            if (key.label.toString().length() > 1) {
                paint.setTextSize(30);
                canvas.drawText(key.label.toString(), key.x
                        + (key.width / 2) - 15, key.y + (key.height / 2)
                        + 10, paint);
            } else {
                canvas.drawText(key.label.toString(), key.x
                        + (key.width / 2) - 10, key.y + (key.height / 2)
                        + 10, paint);
            }
        }
    }

}

public MyKeyBoardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    this.context = context;

}

}

【问题讨论】:

  • 嗯,你试过不调用super.onDraw吗?这将阻止底层键盘自行绘制..假设您想自己完成所有绘图..
  • 你找到任何解决方案了吗@luckysing

标签: android android-layout android-view android-custom-view android-keypad


【解决方案1】:

如果您覆盖 onDraw(),您应该先绘制背景,然后再绘制文本。

public class MyKeyboardView extends android.inputmethodservice.KeyboardView {

    Context context;
    public MyKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context ;
    }



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

         Paint paint = new Paint();
    Typeface font = Typeface.createFromAsset(context.getAssets(),
            "fonts/Hippie.otf");
    paint.setTypeface(font);
    paint.setTextSize(40);




        List<Key> keys = getKeyboard().getKeys();
        for(Key key: keys) {

     if(key.pressed){
                NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.glow);
                npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
                npd.draw(canvas);
            if(key.label != null)
                canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
     }else if(key.modifier){  // boolean that defines key is function key

            NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.btn_keyboard_special);
            npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
            npd.draw(canvas);
            if(key.label != null)
                canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
      }


        break;
        }
    }

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多