Frage

I am trying to accept the letters in the edit text(after typing every letter,Toast should apper that'You pressed a key etc' and also the letter must be displayed on the edit text view)

This is what i did to capture the KeyStrokes but it doesnt seem working as neither of the toast or Log entry is created.

EditText txt=(EditText) findViewById(R.id.editText1);
txt.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_A && event.getAction() == KeyEvent.ACTION_DOWN) {
                   Toast.makeText(MainActivity.this, "YOU PRESSED A", Toast.LENGTH_SHORT).show();
                   Log.d("HEY", "You pressed A");
                    return false;
                }
                return false;
            }
        });

This is written in onCreate Method.

This is just an test code,if this works i will modify it use as a remotekeyboard for pc.(via Bluetooth)

War es hilfreich?

Lösung

The best way is to use the TextWatcher class

EditText txt = (EditText) findViewById(R.id.editText1);

TextWatcher myTextWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Toast.makeText(MainActivity.this, "YOU PRESSED A", Toast.LENGTH_SHORT).show();
        Log.d("HEY", "You pressed A");
    }
};
txt.addTextChangedListener(myTextWatcher);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top