java - Reset Buttons shows 0 score but doesn't really clean the answers -
i have simple quiz app users can see score scorebutton , reset score resetbutton. questions in radioboxes.the problem when user clicks on resetbutton, button shows 0 doesn't clean radioboxes user had choose start beginning. miss in code can't figure out. appreciated lot!
mainactivity.java
public class mainactivity extends appcompatactivity { string name; int score = 0; button submitbutton; button resetbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //user input name final edittext namefield = (edittext)findviewbyid(r.id.namefield); name = namefield.gettext().tostring(); //submitbutton shows user score submitbutton = (button) findviewbyid(r.id.submitbutton); submitbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { submitbutton.settext("your score is:" + score); } }); //resetbutton reset score 0 resetbutton = (button) findviewbyid(r.id.resetbutton); resetbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { submitbutton.settext((string.valueof(0))); } }); } //this method called when radio buttons clicked public void onradiobuttonclicked(view view) { boolean checked = ((radiobutton)view).ischecked(); switch (view.getid()) { //display toast message right answers case r.id.firstleftradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score ++; break; case r.id.secondrightradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score++; break; case r.id.thirdleftradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score++; break; case r.id.fourthleftradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score++; break; case r.id.fifthrightradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score++; break; case r.id.sixthleftradiobutton: if (checked) toast.maketext(this, "hooray!your answer right", toast.length_short).show(); score++; break; //display toast message wrong answers case r.id.firstrightradiobutton: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; case r.id.secondleftradiobutton: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; case r.id.thirdrightradiobutton: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; case r.id.fourthrightcheckbox: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; case r.id.fifthleftradiobutton: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; case r.id.sixthrightradiobutton: if (checked) toast.maketext(this, "sorry, try again!", toast.length_short).show(); score--; break; } }
activity_main.xml
<relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <edittext android:id="@+id/namefield" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontfamily="sans-serif-light" android:inputtype="text" android:hint="name" android:textcolor="#ef6c00" android:textsize="15sp"/> <textview android:id="@+id/welcomemessage" style="@style/welcomescreentext" android:fontfamily="sans-serif-light" android:text="welcome hungry history!\n let's started!"/> <textview android:id="@+id/firstquestion" style="@style/questionsstyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/welcomemessage" android:paddingleft="5dp" android:text="who born in ancient city stagira, greece?"/> <textview android:id="@+id/secondquestion" style="@style/questionsstyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/firstleftradiobutton" android:paddingleft="5dp" android:text="who said in last speech:with malice toward none;...let strive on finish work in;to bind nation's wounds;into care him shall have borne battle , widow , orphans?"/> <textview android:id="@+id/thirdquestion" style="@style/questionsstyle" android:layout_below="@+id/secondleftradiobutton" android:paddingleft="5dp" android:text="where lushan rebellion took place?"/> <textview android:id="@+id/fourthquestion" style="@style/questionsstyle" android:layout_below="@+id/thirdleftradiobutton" android:text="who famous exemplar of absolute monarchy in france?"/> <textview android:id="@+id/fifthquestion" style="@style/questionsstyle" android:layout_below="@+id/fourthleftradiobutton" android:text="when alexander great lived?"/> <textview android:id="@+id/sixthquestion" style="@style/questionsstyle" android:layout_below="@+id/fifthleftradiobutton" android:text="where albert einstein studied?"/> <textview android:id="@+id/seventhquestion" style="@style/questionsstyle" android:layout_below="@+id/sixthleftradiobutton" android:text="what main interest of democritus?"/> <radiogroup android:id="@+id/firstgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/firstquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/firstleftradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/firstquestion" android:text="aristotle" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/firstrightradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/firstquestion" android:layout_torightof="@+id/firstleftradiobutton" android:text="pythagoras" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/secondgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/secondquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/secondleftradiobutton" style="@style/radiobuttonstyle" android:layout_alignparentleft="true" android:layout_below="@+id/secondquestion" android:text="william mckinley" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/secondrightradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/secondquestion" android:layout_torightof="@+id/secondleftradiobutton" android:text="abraham lincoln" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/thirdgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/thirdquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/thirdleftradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/thirdquestion" android:text="china" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/thirdrightradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/thirdquestion" android:layout_torightof="@+id/thirdleftradiobutton" android:text="thailand" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/fourthgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/fourthquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/fourthleftradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/fourthquestion" android:text="louis xiv" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/fourthrightcheckbox" style="@style/radiobuttonstyle" android:layout_below="@+id/fourthquestion" android:layout_torightof="@+id/fourthleftradiobutton" android:text="michael i" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/fifthgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/fifthquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/fifthleftradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/fifthquestion" android:text="330-323 bc" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/fifthrightradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/fifthquestion" android:layout_torightof="@+id/fifthleftradiobutton" android:text="336-323 bc" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/sixthgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/sixthquestion" android:orientation="horizontal"/> <radiobutton android:id="@+id/sixthleftradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/sixthquestion" android:text="university of zurich" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/sixthrightradiobutton" style="@style/radiobuttonstyle" android:layout_below="@+id/sixthquestion" android:layout_torightof="@+id/sixthleftradiobutton" android:text="university of germany" android:onclick="onradiobuttonclicked"/> <radiogroup android:id="@+id/seventhgroupradiobuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/seventhquestion" android:orientation="horizontal"> <radiobutton android:id="@+id/seventhleftradiobutton" style="@style/radiobuttonstyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mathematics-astronomy" android:onclick="onradiobuttonclicked"/> <radiobutton android:id="@+id/seventhrightradiobutton" style="@style/radiobuttonstyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="philosophy-psychology" android:onclick="onradiobuttonclicked"/> </radiogroup> <textview android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true"/> <button android:id="@+id/submitbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/seventhgroupradiobuttons" android:layout_marginbottom="3dp" android:background="@color/backgroundcolor" android:text="submit" android:textcolor="@color/textcolor" android:onclick="onclick"/> <button android:id="@+id/resetbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/submitbutton" android:layout_marginbottom="3dp" android:onclick="onclick" android:background="@color/backgroundresetcolor" android:textcolor="@color/textcolor" android:text="reset" android:textallcaps="true"/> </relativelayout>
put
radiogroup.clearcheck();
to resetbutton onclick
.
this might you.
Comments
Post a Comment