please Click here to see the problematic image
i am beginner in android I need some idea to solve my doubt and my doubt is when one switch button is active then the other two switch button should remain inactive or disabled how can we perform this activity can someone provide me any ideas...? I've just currently inserted the switch buttons how should i perform this task.
i just used relative layout in android programming
My sourcecode:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openThird();
}
});
btnswitch=(Switch)findViewById(R.id.switch1);
btnswitch=(Switch)findViewById(R.id.switch2);
btnswitch=(Switch)findViewById(R.id.switch3);
}
public void openThird()
{
Intent toy=new Intent(this,Third.class);
startActivity(toy);
}
Guide me through this.
First of all, you are assigning three different gui elements (your Switch
es) to the same java variable / class attribute of your Activity
, that means only the last definition can actually be handled in your code.
Don't do
btnswitch = (Switch)findViewById(R.id.switch1);
btnswitch = (Switch)findViewById(R.id.switch2);
btnswitch = (Switch)findViewById(R.id.switch3);
instead, provide more class attributes to your activity to handle each Switch
separately
btnswitchOne = (Switch)findViewById(R.id.switch1);
btnswitchTwo = (Switch)findViewById(R.id.switch2);
btnswitchThree = (Switch)findViewById(R.id.switch3);
Then add listeners to them (example for the first one only):
btnSwitchOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do your desired action on switch on/off
if (isChecked) {
// if the first switch gets checked, uncheck the others
btnSwitchTwo.setChecked(false);
btnSwitchThree.setChecked(false);
} else {
/* since you are switching them off by code depending on other's state,
* either skip the else-block entirely or print some debug messages
*/
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.