The first "Switch" button does not affect the second

I have a little problem, and now I'm working with a toggle button, and the first toggle button is for day or night indication, and the second toggle button is to turn on or off the light. Then my requirement is when the day comes, and the second switch button should not work, and then when the final switch button should work, and this should indicate whether the indicators are on. And my code

final ToggleButton tb = (ToggleButton) findViewById(R.id.togglebutton); tb.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is "+tb.getText().toString(), Toast.LENGTH_LONG).show(); if(tb.getText().toString().equals("ON")) { final ToggleButton tb1= (ToggleButton) findViewById(R.id.togglebutton1); tb1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is "+tb1.getText().toString(), Toast.LENGTH_LONG).show(); }}); } else { Toast.makeText(screen4.this,"It is day" , Toast.LENGTH_LONG).show(); finish(); } } }); 

Can someone help me make the second button not work when the first button is off. thanks in advance

+4
source share
2 answers

This worked for me:

  <ToggleButton android:id="@+id/togglebutton" android:layout_width="150px" android:layout_height="50px" android:textOn="DAY" android:textOff="NIGHT" /> <ToggleButton android:id="@+id/togglebuttontwo" android:layout_width="150px" android:layout_height="50px" android:textOn="ON" android:textOff="OFF" /> 

Code:

  final ToggleButton tb = (ToggleButton) findViewById(R.id.togglebutton); tb.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is " + tb.getText().toString(), Toast.LENGTH_LONG).show(); ToggleButton tbtwo = (ToggleButton) findViewById(R.id.togglebuttontwo); if(tb.getText().equals("DAY")) { tbtwo.setEnabled(false); } else tbtwo.setEnabled(true); } }); 

enter image description here

+2
source

try it.

 final ToggleButton tb = (ToggleButton) findViewById(R.id.togglebutton); tb.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is "+tb.getText().toString(), Toast.LENGTH_LONG).show(); if(tb.getText().toString().equals("ON")) { final ToggleButton tb1= (ToggleButton) findViewById(R.id.togglebutton1); tb1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is "+tb1.getText().toString(), Toast.LENGTH_LONG).show(); }}); } else { Toast.makeText(screen4.this,"It is day" , Toast.LENGTH_LONG).show(); final ToggleButton tb1= (ToggleButton) findViewById(R.id.togglebutton1); tb1.setEnabled(false); finish(); } } }); 

your code does not work the way you want, because you get a refrence toggleButton when it is night and set it to OnClickListener, but otherwise you do nothing, in this case the android will provide default behavior, which is the only reason. so otherwise the condition will either disable it, or make it not Togglable or something

+1
source

All Articles