android - How to implemente OnClickListener in an activity? -
in application have simple activity 3 buttons, , not set individual onclicklistener each button, decided implement in activity, doesn't work.
here code
public class mainactivity extends activity implements view.onclicklistener{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } private void checkforviolations(){ } @override public void onclick(view v) { switch (v.getid()){ case r.id.btncheck: checkforviolations(); break; case r.id.btnviewallviolations: intent violationlistintent = new intent(mainactivity.this,violationlistactivity.class); startactivity(violationlistintent); break; case r.id.btnsettings: intent settingsintent = new intent(mainactivity.this, violationlistactivity.class); startactivity(settingsintent); break; } } }
am missing something, because program not stop in onclick method when debug app.
you have set view's listener activity. so, should this:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); view btncheck = findviewbyid(r.id.btncheck); btncheck.setonclicklistener(this); view btnviewallviolations = findviewbyid(r.id.btnviewallviolations); btnviewallviolations.setonclicklistener(this); view btnsettings = findviewbyid(r.id.btnsettings); btnsettings.setonclicklistener(this); }
Comments
Post a Comment