How to toggle focus between buttons without losing focus on clicking button
NickName:Henrik R Ask DateTime:2014-11-18T20:04:01

How to toggle focus between buttons without losing focus on clicking button

Im doing speed game where Thread changes focus on 4 different buttons. I want only the thread to change focuses on the buttons. The problem I am having is that when the thread for example chooses blue button to focus and if I press red button then the blue button loses focus. I can see this clearly because my buttons have different pictures when default, pressed and focused. So is there anyway to force keep focus on the button I have chosen even tho I press other button. Here is my code:

Thread:

public void run() {
        while (aika > 0 && isKaynnissa()) {
            try {
                Thread.sleep(aika);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            debug.setText(Integer.toString(aika));
                            if (isKaynnissa()) {
                            arvoNappi();
                            }
                        }
                    });

            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            if (aika < 550) {
                uusiaika = 3;
            } else if (aika < 450) {
                uusiaika = 2;
            } else if (aika < 350) {
                uusiaika = 1;
            }
            aika = aika - uusiaika;
        }
        aika = aloitusaika;
        unfocusButtons();
        arvotutLuvut.clear();

    }

Random focus chooser:

public void arvoNappi() {
        Random random = new Random();
        int uusirandom = (int) (4 * random.nextDouble() + 1);
        while (uusirandom == painettavaNappi) {
            uusirandom = (int) (4 * random.nextDouble() + 1);
        }
        painettavaNappi = uusirandom;
        tvrandom.setText(Integer.toString(painettavaNappi));
        if (painettavaNappi == 1) {
            arvotutLuvut.add(1);
            btnPun.setFocusable(true);
            btnPun.requestFocusFromTouch();
        } else if (painettavaNappi == 2) {
            arvotutLuvut.add(2);
            btnVio.setFocusable(true);
            btnVio.requestFocusFromTouch();
        } else if (painettavaNappi == 3) {
            arvotutLuvut.add(3);
            btnVih.setFocusable(true);
            btnVih.requestFocusFromTouch();
        } else {
            arvotutLuvut.add(4);
            btnSin.setFocusable(true);
            btnSin.requestFocusFromTouch();
        }
    }

Copyright Notice:Content Author:「Henrik R」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26993929/how-to-toggle-focus-between-buttons-without-losing-focus-on-clicking-button

Answers
lokifacio 2014-11-18T12:33:22

Every time you focus a button inside your thread, store a reference to that button in a variable accesible by your class.\n\nThen, on your button's onClickListener in addition to your program logic, request the focus to the last focused button.\n\nNot sure if it will prevent your on click from changing the image of the button and causing some glitches. If that would be the case, you can try to perform your click detection using a TouchListener instead.",


More about “How to toggle focus between buttons without losing focus on clicking button” related questions

How to toggle focus between buttons without losing focus on clicking button

Im doing speed game where Thread changes focus on 4 different buttons. I want only the thread to change focuses on the buttons. The problem I am having is that when the thread for example chooses b...

Show Detail

How to open a new JFrame without losing focus and editing?

By clicking on the button opens a new frame and until it is fully loaded (including execute a query in the database) I can not continue to work in the initial JFrame. (edit fields). How to organiz...

Show Detail

Button not losing focus

I have a WPF application running on a Windows 8.1 pro tablet. When I click on a button in the application the click event gets fired but the button is not losing his focus (it stays in the same col...

Show Detail

CSS, jQuery - delay on losing focus with min-height

I want to create a delay upon losing focus of a textarea. Currently I have: &lt;textarea name="description"&gt;&lt;/textarea&gt; My CSS reads .my-form textarea { height: 35px; } .my-fo

Show Detail

Winforms Prevent DataGridView from losing focus

I am writting code in winforms using vb.net and I am trying to prevent a datagridview from losing focus when certain user business rule error conditions exist. When the errors exist I have code in...

Show Detail

How do I prevent TextBox or RichEditBox from losing focus after clicking on a disabled button?

The title says it all: "How do I prevent TextBox or RichEditBox from losing focus after clicking on a disabled button?" I tried setting AllowFocusOnInteraction to false, but that only works when the

Show Detail

Toggle focus onclick of a button

I have a form inside a panel. The panel opens and closes when a button is clicked. When the panel is opened [i.e., with the click of the button], I want to focus on the first input text box in the ...

Show Detail

Toggle Button Comes in to Focus Only after Clicking the List Item in the ListView

I have a Listview With two Textviews and toggle Button and i have ontoggleChangeListener for the Toggle Button. but if i click on the toggle button without clicking the list item it is not coming i...

Show Detail

Toggle focus between buttons in a side menu in React using autoFocus

I'm trying to toggle focus between buttons in a side menu. The button gets clicked should be focused. My side menu code: import React, { useRef } from &quot;react&quot;; import styles from &quot;./

Show Detail

Set textfield value without losing the focus

how can I change the value of a textarea or textfield // Clean button $('#textfield').val(''); without losing the focus? I create at the moment an webApp and the smartphone (android) loosing ...

Show Detail