java - Two RadioGroups with only one selected RadioButton in each of them -
i have 2 radiogroups in layout. want 1 radiobutton should checked @ time in radiogroup. i've tried many methods, none of them work properly. can check every radiobutton in radiogroups.
public class mainactivity extends actionbaractivity implements view.onclicklistener, radiogroup.oncheckedchangelistener { string x; char y; int c=1; string num,n,type; int d,b,o,h; string dec,bin,hex,oct; edittext number; edittext result; button convert, clear; radiogroup rgfrom, rgto; int convertedresult; int from=r.id.r10from; int to=r.id.r2to; string stnumber; //liczba przed przeliczeniem - string int numbresult; //już int string iterresult; //liczba po przeliczeniu - string int convresult; //już int //int id = ((radiogroup).findviewbyid( r.id.rgfrom )).getcheckedradiobuttonid(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initialize(); } private void initialize() { number = (edittext) findviewbyid(r.id.etnumber); result = (edittext) findviewbyid(r.id.etresult); convert = (button) findviewbyid(r.id.bcalculate); clear = (button) findviewbyid(r.id.breset); convert.setonclicklistener(this); clear.setonclicklistener(this); rgfrom = (radiogroup) findviewbyid(r.id.rgfrom); rgto = (radiogroup) findviewbyid(r.id.rgto); rgfrom.setoncheckedchangelistener(this); rgto.setoncheckedchangelistener(this); } @override public void onclick(view view) { // = (string)findviewbyid(r.id.rgfrom).getselecteditem(); // int id = ((radiogroup)findviewbyid( r.id.rgfrom )).getcheckedradiobuttonid(); //from = getfrom(id); switch (view.getid()){ case r.id.r2from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r8from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r10from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r16from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r2to: = rgto.getcheckedradiobuttonid(); break; case r.id.r8to: = rgto.getcheckedradiobuttonid(); break; case r.id.r10to: = rgto.getcheckedradiobuttonid(); break; case r.id.r16to: = rgto.getcheckedradiobuttonid(); break; case r.id.bcalculate: stnumber = number.gettext().tostring(); //iterresult = result.gettext().tostring(); //calculate(); break; case r.id.breset: //reset(); break; } } @override public void oncheckedchanged(radiogroup rgfrom, int i) { switch (view.getid()){ case r.id.r2from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r8from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r10from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r16from: = rgfrom.getcheckedradiobuttonid(); break; case r.id.r2to: = rgto.getcheckedradiobuttonid(); break; case r.id.r8to: = rgto.getcheckedradiobuttonid(); break; case r.id.r10to: = rgto.getcheckedradiobuttonid(); break; case r.id.r16to: = rgto.getcheckedradiobuttonid(); break; } } }
my layout:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.converter_numeralsystem.app.mainactivity"> <textview android:layout_margintop="20dp" android:text="@string/number" android:textsize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvnumber" /> <textview android:layout_margintop="20dp" android:layout_below="@id/tvnumber" android:text="@string/result" android:textsize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvresult" /> <edittext android:id="@+id/etnumber" android:layout_marginleft="5dp" android:hint="@string/enter_numb" android:singleline="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toendof="@id/tvnumber" android:layout_alignbottom="@id/tvnumber"/> <edittext android:id="@+id/etresult" android:layout_marginleft="5dp" android:hint="@string/et_result" android:singleline="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toendof="@id/tvnumber" android:layout_alignbottom="@id/tvresult"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="@string/from" android:id="@+id/tvfrom" android:layout_below="@id/tvresult" android:layout_margintop="20dp" /> <radiogroup android:weightsum="100" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tvfrom" android:id="@+id/rgfrom" android:layout_margintop="10dp"> <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <radiobutton android:layout_weight="50" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r2from" android:text="@string/dwa"/> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r8from" android:text="@string/osiem" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_marginright="75dp" /> <radiobutton android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r10from" android:text="@string/dziesiec" android:layout_below="@id/r2from" android:layout_alignstart="@id/r2from" /> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r16from" android:text="@string/szesnascie" android:layout_alignparentright="true" android:layout_below="@id/r8from" android:layout_alignstart="@id/r8from"/> </relativelayout> </radiogroup> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="@string/to" android:id="@+id/tvto" android:layout_below="@id/rgfrom" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="20dp" /> <radiogroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvto" android:layout_margintop="10dp" android:id="@+id/rgto"> <relativelayout android:id="@+id/rel2" android:layout_width="fill_parent" android:layout_height="wrap_content"> <radiobutton android:checked="true" android:layout_weight="50" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r2to" android:text="@string/dwa"/> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r8to" android:text="@string/osiem" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_marginright="75dp" /> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r10to" android:text="@string/dziesiec" android:layout_below="@id/r2to" android:layout_alignstart="@id/r2to" /> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/r16to" android:text="@string/szesnascie" android:layout_alignparentright="true" android:layout_below="@id/r8to" android:layout_alignstart="@id/r8to"/> </relativelayout> </radiogroup> <linearlayout android:layout_margintop="10dp" android:weightsum="100" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/rgto"> <button android:layout_weight="30" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/calculate" android:id="@+id/bcalculate" android:layout_toleftof="@+id/breset" /> <button android:layout_weight="70" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/reset" android:id="@+id/breset" android:layout_alignbottom="@+id/bcalculate" /> </linearlayout> </relativelayout>
remove relativelayout
radigroup
. code like:
<radiogroup android:id="@+id/rgfrom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:weightsum="100" > <radiobutton android:id="@+id/r2from" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:text="dwa" /> <radiobutton android:id="@+id/r8from" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="75dp" android:text="osiem" /> <radiobutton android:id="@+id/r10from" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="dziesiec" /> <radiobutton android:id="@+id/r16from" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="szesnascie" /> </radiogroup>
this resolve issue facing multiple buttons being checked in same group. however, create small issue in ui. think want 2 buttons in 1 line, , other 2 buttons in different line. far know, have implement own custom layout that. check this post details. check here, too. hope helps bit.
Comments
Post a Comment