2010-07-06 72 views
7

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

+0

您有相关设置任何更迭定制keybackgrounds为键盘上的一个键? – Wouter 2012-06-23 16:37:13

回答

5

如果你正在写自己的输入法,请尝试使用的前景图像的绘制(与Android:在Java的XML或Key.icon keyIcon)等于整个密钥的大小。这基本上等同于设置单个键的背景图像。当然你也必须在图像中包含前景。

您还必须使用没有填充的背景图像,因此它不会在边缘周围窥视。看到这个帖子就如何做到这一点的详细信息:how does a 9patch png work in android apps

巴里

+0

这个任何示例代码? – usman 2016-02-27 06:49:46

2

我一直在尝试这样做。关键背景是在KeyboardView类的onBufferDraw()中绘制的。问题是它是一个私有方法,所以你不能用一个子类来覆盖它。所以我尝试复制KeyboardView并修改它,但它使用com.android.internal.R资源,外部应用程序无法访问它。所以这种方法不起作用。

在这一点上,它开始看起来像我必须从窗口扔出Android的键盘类,并从头开始写所有的东西 - 所有B/C我无法改变空间键的背景图像。荒谬。

+0

从头开始并不困难。实际上,您可以将KeyboardView.java从开放源代码复制/粘贴到KeyboardView的新子类中,并开始对其进行自定义以适合您的目的。 – 2012-01-27 20:09:40

+0

Android中的很多类都无法自定义。如果Android的架构使用委托(或侦听器)并且允许更多的点替换辅助类,那么完成会更容易。 – Huperniketes 2013-05-05 01:49:30

+0

我建议复制/粘贴KeyboardView.java到您自己的自定义键盘视图类中。这样你就可以预先写好所有的功能,你可以覆盖任何你需要的功能。这就是我所做的。 – 2014-03-29 02:27:23

4

我定制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 = (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); 
    } 

} 

}

实现效果如下 enter image description here

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