Please find the below solution..It will workable for you
public class Example extends Activity implements OnClickListener {
private ToggleButton button2;
private ToggleButton button1;
private ToggleButton button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
button1 = (ToggleButton) findViewById(R.id.toggleButton1);
button2 = (ToggleButton) findViewById(R.id.toggleButton2);
button3 = (ToggleButton) findViewById(R.id.togglebutton3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.toggleButton1:
if(button1.isChecked())
{
button1.setChecked(true);
Toast.makeText(Example.this, "BUTTON1 UNCHECKED", Toast.LENGTH_LONG).show();
}
else
{
button1.setChecked(false);
Toast.makeText(Example.this, "BUTTON1 CHECKED", Toast.LENGTH_LONG).show();
}
break;
case R.id.toggleButton2:
if(button2.isChecked())
{
button2.setChecked(true);
Toast.makeText(Example.this, "BUTTON2 UNCHECKED", Toast.LENGTH_LONG).show();
}
else
{
button2.setChecked(false);
Toast.makeText(Example.this, "BUTTON2 CHECKED", Toast.LENGTH_LONG).show();
}
break;
case R.id.togglebutton3:
if(button3.isChecked())
{
button3.setChecked(true);
Toast.makeText(Example.this, "BUTTON3 UNCHECKED", Toast.LENGTH_LONG).show();
}
else
{
button3.setChecked(false);
Toast.makeText(Example.this, "BUTTON3 CHECKED", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…