android - How to send a stream of characters on long press of button -
how send stream of characters on long press of button. here code on longclick of button,
btndown.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { sendmessage("s");//here wanted send example sssss on long press of button } });
i want send stream of character on long press of button until user releases button.
try make custom listner
string mstring=""; public class continousrepeatlistener implements view.ontouchlistener { private handler handler = new handler(); private int initialinterval; private final int normalinterval; private final view.onclicklistener clicklistener; private runnable handlerrunnable = new runnable() { @override public void run() { handler.postdelayed(this, normalinterval); clicklistener.onclick(downview); } }; private view downview; /** * @param initialinterval interval after first click event * @param normalinterval interval after second , subsequent click * events * @param clicklistener onclicklistener, called * periodically */ public continousrepeatlistener (int initialinterval, int normalinterval, view.onclicklistener clicklistener) { if (clicklistener == null) throw new illegalargumentexception("null runnable"); if (initialinterval < 0 || normalinterval < 0) throw new illegalargumentexception("negative interval"); this.initialinterval = initialinterval; this.normalinterval = normalinterval; this.clicklistener = clicklistener; } public boolean ontouch(view view, motionevent motionevent) { switch (motionevent.getaction()) { case motionevent.action_down: handler.removecallbacks(handlerrunnable); handler.postdelayed(handlerrunnable, initialinterval); downview = view; downview.setpressed(true); clicklistener.onclick(view); return true; case motionevent.action_up: case motionevent.action_cancel: handler.removecallbacks(handlerrunnable); downview.setpressed(false); downview = null; return true; } return false; } }
and use this
btndown.setontouchlistener(new continousrepeatlistener(400, 100, new view.onclicklistener() { @override public void onclick(view view) { // code execute repeatedly mstring += "s"; tvstring.settext(mstring); } }));
Comments
Post a Comment