You can do the same with Message instead of Runnable, subclassing
Handler.  Works out to be about the same number of lines, though.

On Aug 24, 3:22 pm, Jason Braucht <[email protected]> wrote:
> http://developer.android.com/resources/articles/timed-ui-updates.html
> describes how to use android.os.Handler to create repeating events.  I
> used that along with a an OnTouchListener to repeat the button's
> action every 100ms until the button is released.
>
> Below is a snippet of code that demonstrates the basic idea.  If you
> find any flaws with this or come up with a better approach, please let
> me know.
>
> public MyActivity extends Activity
> {
>     private Handler mHandler = new Handler();
>
>     private Runnable mUpdateTask = new Runnable()
>     {
>         public void run()
>         {
>             Log.i("repeatBtn", "repeat click");
>             mHandler.postAtTime(this, SystemClock.uptimeMillis() +
> 100);
>         }
>     };
>
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         Button repeatButton = (Button)
> findViewById(R.id.repeatButton);
>         repeatButton.setOnTouchListener(new OnTouchListener()
>         {
>             public boolean onTouch(View view, MotionEvent motionevent)
>             {
>                 int action = motionevent.getAction();
>                 if (action == MotionEvent.ACTION_DOWN)
>                 {
>                     Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
>                     mHandler.removeCallbacks(mUpdateTask);
>                     mHandler.postAtTime(mUpdateTask,
> SystemClock.uptimeMillis() + 100);
>                 }
>                 else if (action == MotionEvent.ACTION_UP)
>                 {
>                     Log.i("repeatBtn", "MotionEvent.ACTION_UP");
>                     mHandler.removeCallbacks(mUpdateTask);
>                 }
>                 return false;
>             }
>         });
>     }
>
> }
>
> On Aug 23, 6:15 pm, ls02 <[email protected]> wrote:
>
> > Does android haveauto repeat buttonthat constantly sends events when
> > the button is pressed down? If it does not have such button how do I
> > implement it?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to