簡體   English   中英

如何為Android自定義鍵盤設置不同的按鍵背景

[英]How to set different background of keys for Android Custom Keyboard

我正在開發自定義鍵盤應用程序

這是用戶選擇藍色時鍵的背景顏色

如果用戶選擇綠色,這應該是背景色

這是軟鍵盤中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());

}

我不知道如何為特定鍵設置背景圖像。

例如,有一個可下載小型項目,用於創建自定義數字鍵盤。 到 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 塊圖像並執行以下操作。 請注意,帶有圖標的兩個鍵無法正確呈現,因為圖標未定義為 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);
        }
    }
}    

更換鑰匙

我創建了一個鍵盤應用程序,在其中使用了KeyboardViewKeyBackground屬性,如下所示:

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

保持簡單,你應該創建類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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM