java - Detecting the location where a button was clicked -


i wondering if there way know button tapped, , take different actions based on user tapped it. like:

foobtn.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view view) {         switch(onclicklocation){             case left:                 foo();                 break;             case right:                 bar();                 break;             case middle:                 baz();                 break;         }     } }); 

not using onclicklistener. ontouchlistener gives motionevent can use determine actual touch event occurred.

for example, here register both onclicklistener , ontouchlistener on same view (called row):

row.setonclicklistener(this);  if (build.version.sdk_int >= build.version_codes.lollipop) {   row.setontouchlistener(new view.ontouchlistener() {     @targetapi(build.version_codes.lollipop)     @override     public boolean ontouch(view v, motionevent event) {       v         .findviewbyid(r.id.row_content)         .getbackground()         .sethotspot(event.getx(), event.gety());        return(false);     }   }); } 

in case, don't need know widget touched processing click, need know widget touched adjusting rippledrawable background, ripple appears emanate user touched. returning false ontouch() means not consuming touch event, , onclick() method called.

in case, either:

  • do actual work in ontouch(), or

  • cache last-seen touch point in ontouch() not work until onclick()

my gut tells me latter should give better results (e.g., won't misinterpret long-click), have not tried doing seeking.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -