A
A
artem2021-01-24 22:43:24
Android
artem, 2021-01-24 22:43:24

How to disable a button?

Hello, I've run into this problem.

I have three buttons, on each I have a dawn and up touch handler, after releasing my finger from the button, the thread falls asleep to me, then the function that updates them should start. At this time, if you press the button and release it again, the thread falls asleep again, that is, an endless dream of the thread is obtained.

switch (view.getId()) {
                case R.id.answer1:
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        int trueNum = 0;
                        for (int i = 0; i < 15; i++) {
                            if (text1.equals(questions.getTrueList(i))) {
                                answer1.setBackgroundDrawable(getDrawable(R.drawable.button_levels_true));
                                trueNum = 1;
                            }
                        }
                        if (trueNum == 0) answer1.setBackgroundDrawable(getDrawable(R.drawable.button_levels_false));
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        try {
                            Thread.sleep(600);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        changeQuestion();
                    }
                    break;

                case R.id.answer2:
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        int trueNum = 0;
                        for (int i = 0; i < 15; i++) {
                            if (text2.equals(questions.getTrueList(i))) {
                                answer2.setBackgroundDrawable(getDrawable(R.drawable.button_levels_true));
                                trueNum = 1;
                            }
                        }
                        if (trueNum == 0) answer2.setBackgroundDrawable(getDrawable(R.drawable.button_levels_false));
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        try {
                            Thread.sleep(600);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        changeQuestion();
                    }
                    break;

                case R.id.answer3:
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        int trueNum = 0;
                        for (int i = 0; i < 15; i++) {
                            if (text3.equals(questions.getTrueList(i))) {
                                answer3.setBackgroundDrawable(getDrawable(R.drawable.button_levels_true));
                                trueNum = 1;
                            }
                        }
                        if (trueNum == 0) answer3.setBackgroundDrawable(getDrawable(R.drawable.button_levels_false));
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        try {
                            Thread.sleep(600);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        changeQuestion();
                    }
                    break;
            }

            return true;
        }
    };


public void changeQuestion() {
        answer1.setBackgroundDrawable(getDrawable(R.drawable.button_levels));
        answer2.setBackgroundDrawable(getDrawable(R.drawable.button_levels));
        answer3.setBackgroundDrawable(getDrawable(R.drawable.button_levels));

        questionText.setText(questions.getQuestion());

        text1 = questions.getAnswer1();
        text2 = questions.getAnswer2();
        text3 = questions.getAnswer3();

        answer1.setText(text1);
        answer2.setText(text2);
        answer3.setText(text3);
    }

I need to find a method where you can disable the properties of a button or disable its up handler. How much I googled, I found only setEnabled (false) and that's it.
I'm new to this stuff, by the way.)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-01-24
@402d

Thread.sleep(600); - on the UI thread is not allowed.
take as a basis the single click example
https://stackoverflow.com/questions/5608720/androi...
And it's better to do it through timerTask
developer.alexanderklimov.ru/android/java/timer.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question