如何为Android自定义键盘设置不同的背景背景

我正在处理自定义键盘应用程序

这是用户选择蓝色时按键的背景颜色

如果用户选择绿色,这应该是背景颜色

这是softkeyboard中input.xml背景颜色的代码: –

  @Override public View onCreateInputView() { Log.e("onStartInputView ","On StartInput View Called--"); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String Backgroundcolour = preferences.getString("BackgroundColour",""); Log.e("Brithnesss- -","----"+Backgroundcolour); if(Backgroundcolour.equalsIgnoreCase("black")) { this.mInputView = (KeyboardView) getLayoutInflater().inflate( R.layout.input, null); }else { this.mInputView = (KeyboardView) getLayoutInflater().inflate( R.layout.input1, null); //this.mInputView.setB } this.mInputView.setOnKeyboardActionListener(this); this.mInputView.setKeyboard(this.mQwertyKeyboard); return this.mInputView; } @Override public void onStartInputView(EditorInfo attribute, boolean restarting) { super.onStartInputView(attribute, restarting); // Apply the selected keyboard to the input view. setInputView(onCreateInputView()); } 

我没有得到如何为特定的键设置背景图像。

Solutions Collecting From Web of "如何为Android自定义键盘设置不同的背景背景"

作为一个例子,有一个小的可下载的项目 ,创build一个自定义的数字键盘。 对于那里的CustomKeyboardView类或者你自己定制的键盘类,添加一个像下面这样的方法。 它覆盖了onDraw()方法,并绘制了使用代码7定义的键的背景(本例中为“0”),其他键均为蓝色。

 @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); List<Key> keys = getKeyboard().getKeys(); for (Key key : keys) { if (key.codes[0] == 7) { Log.e("KEY", "Drawing key with code " + key.codes[0]); Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.red_tint); dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); dr.draw(canvas); } else { Drawable dr = (Drawable) context.getResources().getDrawable(R.drawable.blue_tint); dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); dr.draw(canvas); } } } 

有色钥匙

在这种情况下,我没有使用9贴片图像,而只是一些简单的50%透明方形图像,并获得了现有button仅用我想要的颜色着色的效果。 为了获得更多的自定义结果,我可以使我的背景可绘制9 – 补丁的图像,并执行以下操作。 请注意,带有图标的两个键不能正确显示,因为图标没有被定义为9-补丁图像,我没有做任何特别的努力让它们在这个例子中很好地扩展。 我也没有解决使用不同的图像/效果为键的各种状态; 其他人已经表明如何做到这一点。

 @Override public void onDraw(Canvas canvas) { // super.onDraw(canvas); List<Key> keys = getKeyboard().getKeys(); for (Key key : keys) { if (key.codes[0] == 7) { NinePatchDrawable npd = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.red_key); npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); npd.draw(canvas); } else { NinePatchDrawable npd = (NinePatchDrawable) context.getResources().getDrawable(R.drawable.blue_key); npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); npd.draw(canvas); } Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(48); paint.setColor(Color.GRAY); if (key.label != null) { canvas.drawText(key.label.toString(), key.x + (key.width / 2), key.y + (key.height / 2), paint); } else { key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); key.icon.draw(canvas); } } } 

更换了钥匙

我创build了一个键盘应用程序,在KeyboardView使用KeyBackground属性,如下所示:

 <KeyboardView android:keyBackground="@drawable/buttonbgselector" .../> 

要dynamic地做到这一点,我使用下面的代码:

 @Override public View onCreateInputView() { mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null); mInputView.setBackgroundResource(R.drawable.buttonbgselector); mInputView.setOnKeyboardActionListener(this); mInputView.setKeyboard(mQwertyKeyboard); return mInputView; } 

保持简单,你应该使类MyKeyboardView做一些类似这样的破解。

 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) { // int i = 0 ; switch(i) and implement your logic 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; } }