OnLongClick button listener android -
hello :) trying detect when user press long click on button , when releases long click using answer: https://stackoverflow.com/a/10746549/3953319 don't know why longclicklistener called twice me, here code:
button1.setonlongclicklistener(new view.onlongclicklistener() { public boolean onlongclick(view pview) { timecounter = 0; final random rand = new random(); time = rand.nextint(7) + 1; sectime = time * 1000; countdowntimer2 = new countdowntimer(sectime, 1000) { public void ontick(long millisuntilfinished) { } public void onfinish() { mplayer = mediaplayer.create(mainactivity.this, r.raw.windows_8_notify); mplayer.start(); t = new thread() { @override public void run() { try { while (!isinterrupted()) { thread.sleep(1); runonuithread(new runnable() { @override public void run() { timecounter++; textview.settext("your reaction time:" + timecounter + "ms"); } }); } } catch (interruptedexception e) { } } }; t.start(); } }.start(); isspeakbuttonlongpressed = true; return true; } }); button1.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view pview, motionevent pevent) { pview.ontouchevent(pevent); if (pevent.getaction() == motionevent.action_up) { if (isspeakbuttonlongpressed) { if (mplayer == null) { textview.settext("you have released button before sound"); countdowntimer2.cancel(); } else { t.interrupt(); t = null; oldscore = sharedpreferences.getint("highscore", 0); if (timecounter < oldscore) { editor editor = sharedpreferences.edit(); editor.putint("highscore", timecounter); editor.commit(); textview1.settext("your new high score:" + timecounter + "ms"); } textview.settext("your reaction time:" + timecounter + "ms"); mplayer.pause(); mplayer.reset(); mplayer = null; } isspeakbuttonlongpressed = false; } } return false; } }); i ran application , when long click button hear "windows_8_notify" twice, why that? there better way it?
i believe because in ontouchlistener's ontouch method, call pview.ontouch(...), , @ end, return false. return false line indicates ontouchlistener has not handled ontouch event, resorts calling it's default ontouch function, identical pview.ontouch(...) call. however, default function happens call onlongclicklistener's onlongclick method. thus, calls pview.ontouch(...) twice, each time calling onlongclicklistener's onlongclick method.
so. believe can avoid if return true in ontouch method in appropriate cases. in mind, this:
abbreviated
button1.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view pview, motionevent pevent) { boolean longclickoccurred = pview.ontouchevent(pevent); ... if (pevent.getaction()...) { if (isspeakbuttonlongpressed) { ... longclickoccurred = true; } } return longclickoccurred; } }); full rewrite
button1.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view pview, motionevent pevent) { boolean longclickoccurred = pview.ontouchevent(pevent); if (pevent.getaction() == motionevent.action_up) { if (isspeakbuttonlongpressed) { if (mplayer == null) { textview.settext("you have released button before sound"); countdowntimer2.cancel(); } else { t.interrupt(); t = null; oldscore = sharedpreferences.getint("highscore", 0); if (timecounter < oldscore) { editor editor = sharedpreferences.edit(); editor.putint("highscore", timecounter); editor.commit(); textview1.settext("your new high score:" + timecounter + "ms"); } textview.settext("your reaction time:" + timecounter + "ms"); mplayer.pause(); mplayer.reset(); mplayer = null; } isspeakbuttonlongpressed = false; longclickoccurred = true; } } return longclickoccurred; } }); this will(should) stop button recalling pview.ontouchevent(pevent), without interfering other click functions.
(in fact, don't think need boolean variable isspeakbuttonlongpressed, because value identical longclickoccurred when assert pevent.getaction() == motionevent.action_up. unless 1) use isspeakbuttonlongpressed outside of ontouch function, or 2) have other onclicklistener set or that, cause pview.ontouchevent(pevent) return true when long click has not occurred.)
anyway, hope works/helps/was-not-a-convoluted-explanation. luck!
Comments
Post a Comment