First Toggle button is not making influence on the second
I have small issue and that is I am now working with toggle button and the first toggle button is for day or night indication and second toggle button is for indication of light is on or off. Then my requirement is when it is day then second toggle button should not work and then when nite the second toggle button should work and that should indicate whether the lights are on or off. And my code is
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(V开发者_JS百科iew 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 any one help me in making the second button not to work when the first button is off. Thanks in advance
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);
}
});
try this.
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't work in the way you want because, you get the refrence of the toggleButton when its night and set its onClickListener, but in the other case you do nothing, in that case android will provide its default behaviour thats the only reason. so in else condition either disable it or make it not Togglable or something
精彩评论