如何改变任何键的按键背景Android的软键盘 [英] how to change key background of any key in Android soft keyboard

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

问题描述

我想有一些我在键盘与别人不同的密钥。

I want to have some of my keys in the keyboard different from others.

例如,像移,删除​​,空格键下面的照片:

For example,like shift, delete, space key in below photo:

据来自谷歌的参考文件。我们可以通过使用改键的背景安卓keybackground = @绘制/ XXX ,在 input.xml中,但它改变键盘的所有键的背景。

According to the reference documents from google. We can change key's background by use "android:keybackground=@drawable/xxx" in "input.xml", but it change background of all keys in keyboard.

虽然机器人:keyicon在qwerty.xml可以有一个键修改,但只能更换标签。同时,使用安卓keyicon ,图像不能覆盖整个键,图像会比按键背景豆蔻位小

Although "android:keyicon" in qwerty.xml can have single key changed,but it only replace the label. Meanwhile, use "android:keyicon" , the image can not cover entire key , the image will be a litte bit smaller than the key background.

什么是改变某些键的背景下正确的方法是什么?

推荐答案

目前尚不清楚是否了解如何创建自定义键盘与否。如果你不这样做,这里的创建一个自定义的数字键盘小的下载项目。到了CustomKeyboardView班上有或您自己的自定义键盘类,添加下面的方法。它覆盖的OnDraw()方法和绘制与code 7所定义的密钥的背景(在这种情况下,0)的红色和所有其他键蓝色

It's not clear whether you understand how to create a custom keyboard or not. In case you don't, here's a small downloadable project that creates a custom numeric keyboard. To the CustomKeyboardView class there or to your own custom keyboard class, add the following method. 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 take any special effort to make them 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天全站免登陆