java - How do I use button to switch pages in android studio? -
i trying use btn2 switch page mainactivity calcpage.class.
cant seem figure out doing wrong.
i have error lines underneath btn2, onclicklistener, override, , view v
here mainactivity.java
package edu.khershockolivetcollege.ballistic_calculator; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.content.intent; import java.text.decimalformat; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.content_main); button btn1 = (button)findviewbyid(r.id.calculate); button btn2 = (button)findviewbyid(r.id.calculate); final edittext et1 = (edittext)findviewbyid(r.id.muzzletext); final edittext et2 = (edittext)findviewbyid(r.id.rangetext); final textview time = (textview)findviewbyid(r.id.timeanswer); final textview bulletdrop = (textview)findviewbyid(r.id.dropanswer); btn1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { decimalformat f = new decimalformat("##.00"); double x = new integer(et1.gettext().tostring()); double y = new integer(et2.gettext().tostring()); double timetotarget = y / x; double grav = 9.81; double timesquared = timetotarget * timetotarget; double drop = grav * timesquared; time.settext(" " + f.format(timetotarget) + " seconds"); bulletdrop.settext(" " + f.format(drop) + " meters"); } btn2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent i=new intent(context, calcpage.class); startactivity(i); } }); }
}
it seems setting btn2 onclicklistener inside btn1 onclicklistener. if outside , close curly brackets should work.
like this:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.content_main); button btn1 = (button)findviewbyid(r.id.calculate); button btn2 = (button)findviewbyid(r.id.calculate); final edittext et1 = (edittext)findviewbyid(r.id.muzzletext); final edittext et2 = (edittext)findviewbyid(r.id.rangetext); final textview time = (textview)findviewbyid(r.id.timeanswer); final textview bulletdrop = (textview)findviewbyid(r.id.dropanswer); btn1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { decimalformat f = new decimalformat("##.00"); double x = new integer(et1.gettext().tostring()); double y = new integer(et2.gettext().tostring()); double timetotarget = y / x; double grav = 9.81; double timesquared = timetotarget * timetotarget; double drop = grav * timesquared; time.settext(" " + f.format(timetotarget) + " seconds"); bulletdrop.settext(" " + f.format(drop) + " meters"); } }); btn2.setonclicklistener(new onclicklistener() { @override public void onclick (view v){ intent = new intent(context, calcpage.class); startactivity(i); } }); }
Comments
Post a Comment