【问题标题】:How to set different background of keys for Android Custom Keyboard如何为 Android 自定义键盘设置不同的按键背景
【发布时间】:2013-08-15 23:31:00
【问题描述】:

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

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

}

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

【问题讨论】:

  • 我的表格不太正确,你能告诉我如何设置不同的颜色键吗?
  • 我需要为特定的键背景设置背景,而不是为整个键盘设置相同的键背景图像。我急需
  • 嗨@user...我已经完成了彩色按键,但按键中的文字不可见..你能帮帮我吗?

标签: android android-softkeyboard soft-keyboard


【解决方案1】:

例如,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);
        }            
    }
}

在这种情况下,我没有使用 9-patch 图像,而只是一些简单的 50% 透明方形图像,并实现了现有按钮仅用我想要的颜色着色的效果。为了获得更自定义的结果,我可以将我的背景可绘制对象设置为 9-patch 图像并执行以下操作。请注意,带有图标的两个键无法正确呈现,因为图标未定义为 9-patch 图像,并且我没有特别努力让它们在此示例中很好地缩放。我也没有针对按键的不同状态使用不同的图像/效果;其他人已经展示了如何做到这一点。

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

【讨论】:

  • 长按自定义键盘视图的键时如何以编程方式更改弹出背景?
  • 答案中提供的链接没有回答问题。
  • @MaihanNijat,它本身不应该这样做;它只是提供相当标准样式的自定义键盘的便利。答案中明确添加的代码演示了对该项目的简单更改(或官方文档和其他 S/O 帖子中提供的类似代码)以回答 OP 的问题。
  • @scottt 你用什么来关闭键盘?
  • @ste9206 我已经完成了彩色按键,但按键中的文字不可见..您能帮帮我吗??
【解决方案2】:

我创建了一个键盘应用程序,其中我使用了KeyboardView 中的KeyBackground 属性,如下所示:

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

要动态执行此操作,我使用以下代码:

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

【讨论】:

    【解决方案3】:

    为了简单起见,您应该创建类 MyKeyboardView 并做一些类似的 hack。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2011-08-31
      • 2012-07-17
      • 2017-02-19
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      相关资源
      最近更新 更多