如何设置为Android自定义键盘按键的不同的背景 [英] How to set different background of keys for Android Custom Keyboard

查看:3893
本文介绍了如何设置为Android自定义键盘按键的不同的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的自定义键盘应用程序

I am Working on Custom Keyboard Application

这是code为的input.xml 的背景色softkeyboard: -

This is code for background color of input.xml in softkeyboard :-

     @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());

}

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

I am not getting how to set background image for specific key.

推荐答案

作为一个例子,有一个创建一个自定义的数字键盘小下载项目。到CustomKeyboardView班上有或您自己的自定义键盘类中,添加类似下面的方法。它覆盖的onDraw()方法,并绘制与code 7所定义的密钥的背景(在这种情况下,0)的红色和所有其他键蓝色

As an example, there's a small downloadable project that creates a custom numeric keyboard. To the CustomKeyboardView class there or to your own custom keyboard class, add a method like the following. It overrides the onDraw() method and draws the background of the key defined with code 7 (in this case the "0") red and all the other keys blue.

@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%的透明正方形图像,并取得其中现有的按钮仅仅与我想要的颜色的着色效果。为了获得更多的自定义的结果,我可以让我的绘项目9补丁图像和做到以下几点。请注意,这两个键与图标,因为他们不定义为9补丁图像和我没有做任何特别的努力,让他们在这个例子很好地扩展不能正确地呈现。我也没有解决使用不同的图像/对密钥的各种状态效果;别人已经展示了如何做到这一点。

In this case, I didn't use 9-patch images, but just some simple 50% transparent square images and achieved an effect where the existing buttons are merely tinted with the colors I wanted. To get a more custom result, I could make my drawables 9-patch images and do the following. Note that the two keys with icons don't render correctly because they're not defined as 9-patch images and I didn't make any special effort to allow them to scale well for this example. I also didn't address the use of different images/effects for the various states for the keys; others have shown how to do that.

@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);
        }
    }
}    

这篇关于如何设置为Android自定义键盘按键的不同的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆