简体   繁体   中英

Condition for Button

I want to add a condition for a ShowSolution button. When you press the submit button which is my AnswerCheck Method, this is only when you will able be press the Show Solution button. Is there any way to do this? Would Appreciate the help, Thanks.

public void AnswerCheck2(View view) {

        EditText num2 = (EditText) findViewById(R.id.answertext2);
        int val2 = Integer.parseInt(num2.getText().toString());


        area = number3 * number3;

        if (val2 == area) {
            Toast.makeText(this, "The answer is correct", Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, "The answer is incorrect ", Toast.LENGTH_SHORT).show();
        }

    }

    public void ShowSolution2(View view){
        ((TextView) findViewById(R.id.solution2)).setText
                ("The Solution is "+number3 + " * " +number3);

        ((TextView) findViewById(R.id.answer2)).setText("The answer is "+area);
    }


}

If I understand your question correctly, there are multiple ways to enable/display a button depending on a condition, such as:

  • button.setEnabled(true)
  • button.setVisibility(VISIBLE)

where button is the button you got from calling findViewById(id) .

Of course this would also mean that your button was disabled or hidden previously, but it's difficult to offer guidance without further explanation.

From what I understood, you first want user to submit their answer and then only after submitting their answer, want to show solution button.

Then in that case use this:-

Initially In oncreate method :- solutionbutton.setvisibility(false)

Then in answercheck2:- Solutionbutton.setvisibility.setvisibility(true) num2.setfocusable(false)

This way you can also control user from changing there answer later

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM