2010-07-06 110 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