Java – multiple clicks of the same button
•
Java
The idea is that the button can perform one action on the first click and different actions on the second click
button_food = (Button) findViewById(R.id.foodicon_layout); button_travel = (Button) findViewById(R.id.travelicon_layout); button_fuel = (Button) findViewById(R.id.fuelicon_layout); button_fetch = (Button) findViewById(R.id.fetchicon_layout); button_travel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click button_food.setVisibility(View.GONE); button_fuel.setVisibility(View.GONE); button_fetch.setVisibility(View.GONE); } });
In the given example, click button_ When traveling, other buttons will become invisible Click the same button again and I want the other buttons to be visible again
Solution
You can set the visibility of the button by taking the current visibility and switching it
button_food = (Button) findViewById(R.id.foodicon_layout); button_travel = (Button) findViewById(R.id.travelicon_layout); button_fuel = (Button) findViewById(R.id.fuelicon_layout); button_fetch = (Button) findViewById(R.id.fetchicon_layout); button_travel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int visibility = button_food.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE; // Perform action on click button_food.setVisibility(visibility); button_fuel.setVisibility(visibility); button_fetch.setVisibility(visibility); } });
Writing it like this is just a simple way to write if statements
int visibility; if(button_food.getVisibility() == View.VISIBLE){ visibility = View.GONE; } else { visibility = View.VISIBLE; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码