简体   繁体   中英

Making a button disappear after clicking another button

I'm somewhat new to coding and I'm looking for the best way to make two buttons disappear after clicking a different button within the same activity.

Some context: I want the user to be able to click through different options using two buttons that will appear on the left and right of the screen, laid over the map fragment - then, after pressing a 'Go' button, the buttons will disappear.

My research has suggested using either setVisible or making the buttons instance members, but would these not mean that the user would be able to accidentally press the buttons without being able to see them, ie by panning the map where the buttons had previously been visible and still exist invisibly?

If you will use button.setVisibility(View.GONE) , then you can be calm. Doc for View.GONE:

This view is invisible, and it doesn't take any space for layout purposes.

Button b1 = get reference
Button b2 =  get Reference 
Button b3 = get Reference
 .
 .
 .
b3.setOnCLickListner(....

@override
.... onCLick(...)
{
b1.setVisibilty(View.Invisible);
b1.setClickable(false); this will prevent the further clicking the button 
//same for the other button
}
.
.
...

您可以检查VIEW.GONE。它与invisible不同。

set button.setVisibility(View.GONE) so your button will gone and user will not click anymore. Invisible just hide that button but accept all clicks.

Well, I would answer this question in layman terms:-

The button thing works like this

  1. You first create an instance of the class Button as b1 and associate that button instance with the layout element defined by a specific button ID using

      Button b1 = findViewById(R.id.button_id); 
  2. You check if the button is pressed by calling a function on the instance of the button

     b1.setOnCLickListner() 
  3. You write a code for the situation When the button is pressed using the function onClick() as described by

     public void onClick(View v) { // Code here executes on the main thread after user presses the button } 

Now, as you can see, if you want to do anything, you do that inside the onClick() function. Here, in your case, you want to make the button invisible, you can do so by calling the function setVisibility() on the instance of the button (which if you remember is b1 here)

    b1.setVisibility(View.Invisible);

Making a button invisible doesn't just hide it, but also makes it inaccessible to the user.

Button b,b1;
   b=findViewById(R.id.button);
        b1=findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                b1.setVisibility(View.GONE);
            }
        });

//if you get any problem plz comment .

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