Can ToggleButton change the functionality of other buttons in the same Activity?

To summarize, can ToggleButton change what other buttons in the activity do when they switch? If yes, then a more specific explanation of what I want to do is below:

Basically there are three buttons and a toggle button. When the switch switches, pressing any of the three buttons will take a picture and “save it” for that button. When turned off, pressing any of the three buttons simply displays their images. I think I can determine the part of the camera capture, but I need some direction when it comes to the switch.

Any help is appreciated, and I can explain it if necessary.

+4
source share
3 answers

Create a boolean value, for example isToggleOn, which trueor falsedepending on ToggleButton. Then for each of your buttons, you can simply:

Button button1 = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isToggleOn){
            //do one thing
        } else {
            //do other thing
        }      
    }
});
+3
source

What I would do is keep flags for each state at the class level, for example:

public class MyClass {
   private static final int STATE_SAVE = 0;
   private static final int STATE_DISPLAY = 1;
   private int currentState = STATE_DISPLAY; 
   // I made this default for the example, 
   // you should use what makes sense to your project.
}

Then inside the toggle button you can set a flag. To paraphrase this code, since I do not have an open editor:

toggleButton.setOnToggleListener(new OnToggleListener() {
   @Override
   public void onToggled(boolean toggled) {
      if(toggled) {
         currentState = STATE_SAVE;
      } else {
         currentState = STATE_DISPLAY;
      }
   });

Now that the buttons are pressed, you can switch to the state to perform the action:

button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      if(currentState == STATE_SAVE) {
         // Save the image.
      } else if (currentState == STATE_DISPLAY) {
         // Display the image.
      }
   });
+4
source

, . - !

, , toggle. , .

For your purpose, I would suggest defining two sets of listeners - two for each of the three buttons, and then continue to change between them.

+2
source

Source: https://habr.com/ru/post/1648427/


All Articles