android - Add one click listener to two buttons, but perform different action -


well code, 1 button, want have 2 buttons namely btncalcu can me? thank much

button btnsubmit;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_home);     btnsubmit = (button) findviewbyid(r.id.button1);     btnsubmit.setonclicklistener(this); }    @override public void onclick(view v) {     // todo auto-generated method stub     intent intent = new intent(this, home1.class);          startactivity(intent);   } 

}

you can use same listener both buttons , use if statement on id of clicked view know 1 user clicked below:

@override public void onclick(view v) {     if(v.getid() == r.id.button1) {         // id matches submit button's id btnsubmit clicked.         intent intent = new intent(this, home1.class);         startactivity(intent);     } else if(v.getid() == r.id.button2) {         // id matches calculate button's id btncalcu clicked.          intent intent = new intent(this, otherform.class);         startactivity(intent);     } } 

in oncreate method second button , set same way did btnsubmit:

btncalcu = (button) findviewbyid(r.id.button2); btncalcu.setonclicklistener(this); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -