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

标签 android android-softkeyboard soft-keyboard

我正在开发自定义键盘应用程序

This is background color of key when user select blue

If user select green this should be background color

这是软键盘中 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());

}

我不知道如何为特定键设置背景图像。

最佳答案

例如,有一个 small downloadable project创建自定义数字键盘。到那里的 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);
        }            
    }
}

tinted keys

在这种情况下,我没有使用 9 色 block 图像,而是使用了一些简单的 50% 透明方形图像,并实现了现有按钮仅使用我想要的颜色着色的效果。为了获得更加自定义的结果,我可以将我的背景可绘制对象设为 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);
        }
    }
}    

replaced keys

关于android - 如何为Android自定义键盘设置不同的按键背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224520/

相关文章:

android - 即使在 ScrollView 中,键盘也会隐藏我的一半编辑文本和按钮

android - 预估 yield admob降为0

android - 一个简单的一键触摸绘图 View ,但在触摸完成之前不断获得 ACTION_CANCEL

android - 带有 windowSoftInputMode adjustPan 的 ListView 中的 EditText

android - android软键盘上不同键的不同背景颜色

Android软键盘永远不会出现在模拟器中

android - 以编程方式获取强调色(从模块)

java - 为什么条目在 Firebase 数据库中不断循环?

html - 'Next' 的按键事件

android - 如何运行那些 Android SDK 示例应用程序?