Display the contents of a button and make it disappear after a period

Asked

Viewed 119 times

0

I’m totally new with android studio and I’m trying to create a little memory game, the game consists of a series of buttons, a row 4x4, all buttons appear being a "?" when you click the button it displays the number stored inside it, I wanted to make the content displayed inside the button disappear after a few seconds, since it is a memory game, can help me on which function to use to achieve this?

btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    btn1.setText("1");

                }
            });

The btn1.setText("1") command; this taking the contents of the button, it stays forever and does not disappear, ie it does not return to being a ?

2 Doubt, I have a START button, when I clicked on it I wanted my text view that is an Accountant, to start counting the time it would take to close the game.

1 answer

0


btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        btn1.setText("1");
        // O Texto do botão voltará a ser "?" em 5 segundos
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                btn1.setText("?");
            }
        }, 5000); // 5 segundos
    }
});
  • Very good, worked perfectly, the content appears and disappears after 5 seconds, and if the user hits the two buttons with the same content, how do I make them not disappear anymore, only in case of hit.

  • You can put all one buttons in an array (Button[]). When any of them are clicked, loop and check that each button has the same text as the button that was clicked (getText()), if yes, interrupt the loop and not Handler post the method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.