Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set focus to a button

How can I set the focus to a button but without setting setFocusableInTouchMode to true?

I first tried this:

button.requestFocus();

That simply does not work. I then tried this:

button.setFocusableInTouchMode(true);
button.requestFocus();

The problem is, it highlights the button. I don't want the button to be highlighted in touch mode. I simply want the button to be selected when the user hits the enter key or tab key.

To clarify, I simply want the first button to start with focus when not in touch mode.

like image 899
Dan Bray Avatar asked Nov 12 '22 00:11

Dan Bray


1 Answers

I guess, you want to this:

button.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_ENTER:
                    // HIT THE ENTER KEY
                    break;
                case KeyEvent.KEYCODE_TAB:
                    // HIT THE TAB KEY
                    break;
                }
                return false;
            }
        });

if you don't want to this, could you be clear ? Sorry for my English.

like image 97
okarakose Avatar answered Nov 14 '22 21:11

okarakose