android - getCheckedradiobutton always returns -1 -
this code
final radiogroup rg1 = (radiogroup) findviewbyid(r.id.radiogroup1); radiobutton rb1 = (radiobutton) findviewbyid(r.id.radio0); radiobutton rb2 = (radiobutton) findviewbyid(r.id.radio1); radiobutton rb3 = (radiobutton) findviewbyid(r.id.radio2); final checkbox cb1 = (checkbox) findviewbyid(r.id.chkbox1); int id = rg1.getcheckedradiobuttonid(); system.out.println("------------------------|"+id); switch (id) { case r.id.radio0: cb1.setenabled(true); break; case r.id.radio1: cb1.setenabled(true); break; case r.id.radio2: cb1.setenabled(true); break; default: cb1.setenabled(false); break; }
this returns -1(chkbox disabled) , cant seem make work . plus ive tried , assigned individual values through setid radiobutton , doesnt work either.
this xml
<radiogroup android:id="@+id/radiogroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="46dp" > <radiobutton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="radiobutton1" android:checkedbutton ="1" /> <radiobutton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="radiobutton2" android:checkedbutton="2" /> <radiobutton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="radiobutton3" android:checkedbutton="3" /> </radiogroup>
of course won't work. should use listener detect radio button checked, this
rg1.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup group, int checkedid) { system.out.println("------------------------|"+checkedid); switch (checkedid) { ... } } });
Comments
Post a Comment