Autore Topic: Ondraw in KeyboardView  (Letto 337 volte)

Offline jox

  • Nuovo arrivato
  • *
  • Post: 34
  • Respect: 0
    • Mostra profilo
Ondraw in KeyboardView
« il: 13 Dicembre 2015, 17:22:28 CET »
0
Ciao ragazzi! sto creando una keyboard ho seguito il tutorial http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615[/url] e ho creato la mia classe, ora vorrei aggiungere il metodo OnDraw per poter personalizzare le mie chiavi ho creato la seguente classe
Codice (XML): [Seleziona]
public class LatinKeyboardView extends KeyboardView {

    Context context;
    static final int KEYCODE_OPTIONS = -100;
    // TODO: Move this into android.inputmethodservice.Keyboard
    static final int KEYCODE_LANGUAGE_SWITCH = -101;

    public LatinKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }


    @Override
    protected boolean onLongPress(Key key) {
        if (key.codes[0] == Keyboard.KEYCODE_CANCEL) {
            getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
            return true;
        } else {
            return super.onLongPress(key);
        }
    }

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

e ho aggiornato la mia classe principale così
Codice (XML): [Seleziona]
public class CustomKeyboard extends InputMethodService
        implements KeyboardView.OnKeyboardActionListener {


    //private KeyboardView kv;
    private Keyboard keyboard;

    private LatinKeyboardView kv;

    private boolean caps = false;

        //Initializes the interface
    @Override
    public void onInitializeInterface() {
        super.onInitializeInterface();

        keyboard = new Keyboard(this, R.xml.qwerty);
    }


    @Override
    public View onCreateInputView() {
        kv = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.language_input, null);

        //Setting our keyboard on to this view
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        playClick(primaryCode);
        switch(primaryCode){
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
                if(Character.isLetter(code) && caps){
                   code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);
        }
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeUp() {
    }


    private void playClick(int keyCode){
        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
        switch(keyCode){
            case 32:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
                break;
            case Keyboard.KEYCODE_DONE:
            case 10:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
                break;
            case Keyboard.KEYCODE_DELETE:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
                break;
            default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
        }
    }


}
con gli xml language_input
Codice (XML): [Seleziona]
<android.inputmethodservice.KeyboardView
   xmlns:android="[url]http://schemas.android.com/apk/res/android"
   android:id="@+id/keyboard"
   android:layout_alignParentBottom="true"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/minth"
   android:keyBackground="@drawable/colorkey"
   />
  e method.xml
Codice (XML): [Seleziona]
 <?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
<subtype
   android:label="@string/subtype_en_US"
   android:imeSubtypeLocale="en_US"
   android:imeSubtypeMode="keyboard" />
</input-method>
tuttavia la keyboard si blocca..sapreste aiutarmi a capire il motivo?