checkbox - How to sum checkboxes value in android? -
i have problem in java.
package apa.y; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.checkbox; import android.widget.textview; public class myprice extends activity { /** called when activity first created. */ checkbox book,pencil; textview total; int sum=0; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mymain); pencil = (checkbox) findviewbyid(r.id.pencil); book = (checkbox) findviewbyid(r.id.book); total = (textview) findviewbyid(r.id.total); } public void onclick(view v) { sum = 0; if (pencil.ischecked()) {sum += 10;}else{sum +=0;} if (book.ischecked()) {sum += 5;}else{sum +=0;} total.settext("total: "+sum); } }
if book , pencil checkbox , 1 textview, how sum it? existing code didn't work.
what clicking on? checkboxes? need kind of listener views. 2 options provided here: android: checkbox listener
if want use onclick method have now, change this:
public class myprice extends activity implements onclicklistener {
and make sure set onclicklistener both checkboxes:
pencil = (checkbox) findviewbyid(r.id.pencil); pencil.setonclicklistener(this); book = (checkbox) findviewbyid(r.id.book); book.setonclicklistener(this);
Comments
Post a Comment