Change a variable from True to False and vice versa

3

I created a button that when clicked creates the onClick method and makes it a "true" variable

What I want is that if this button is clicked again and the variable ja is true it becomes "false"

I'm using Android Studio, some light?

    
asked by anonymous 29.09.2016 / 02:04

2 answers

2

There are several ways you can do this, but try it this way:

public class mycActivity ... {
  boolean vaiserfalsedepois = false;
  // ...
  meubotao.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
        vaiserfalsedepois = !vaiserfalsedepois;
        // ou 
        if (vaiserfalsedepois === true)
           vaiserfalsedepois = false;
        else
           vaiserfalsedepois = true;
         // e por ai vai
     }
  });
}

I hope it helps!

Light and peace!

    
29.09.2016 / 02:21
5

A simple

variavel = !variavel;

Inside your listener solves your problem.

    
29.09.2016 / 02:30