android - Listener for RadioButton for change Fragments -
i need switch fragments @ application tabs (radiobutton). event listener should use? onclick or oncheckedchanged? if possible example
for single radiobutton
can use onclicklistener
follows:
onclicklistener listener = new onclicklistener() { @override public void onclick(view v) { radiobutton rb = (radiobutton) v; toast.maketext(your_activity.this, rb.gettext(), toast.length_short).show(); } }; radiobutton rb1 = (radiobutton) findviewbyid(r.id.radiobutton1); rb1.setonclicklistener(listener);
in case of radiogroup
need use oncheckedchangelistener
follows :
radiogroup radiogroup = (radiogroup) findviewbyid(r.id.yourradiogroup); radiogroup.setoncheckedchangelistener(new oncheckedchangelistener() { public void oncheckedchanged(radiogroup group, int checkedid) { // checkedid radiobutton selected switch(checkedid) { case r.id.radiobutton1: // switch fragment 1 break; case r.id.radiobutton2: // fragment 2 break; case r.id.radiobutton3: // fragment 3 break; } } });
the oncheckedchanged callback receives id of newly checked button in checkedid parameter.
Comments
Post a Comment