Android, press and hold button to increase continuously

Source: Internet
Author: User

When you hold down the button on the interface, the TextView increases continuously every second. Lift up and stop.

package com.myapp.androidtest2;import java.util.timer;import java.util.timertask;import  android.app.activity;import android.os.bundle;import android.os.handler;import  android.os.message;import android.util.log;import android.view.motionevent;import  android.view.view;import android.view.view.onclicklistener;import android.view.view.ontouchlistener; Import android.widget.button;import android.widget.textview;public class mainactivity  extends activity {private button mbutton;static textview mtextview;static  int i = 1;/** Called when the activity is first  Created. */public void oncreate (bundle savedinstancestate)  {super.onCreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);//  if it's a word switch  Set Ontouchlisennerbuttonlistener b = new buttonlistener (); mbutton =  (Button)  findviewbyid (R.id.button); Mbutton.setonclicklistener (b); Mbutton.setontouchlistener (b); mtextview =  (TextView)  findviewbyid (R.id.textview);} Static class mytask extends timertask {public void run ()  {Message  message = new message (); message.what = 1;mhandler.sendmessage (message);}} Private static handler mhandler = new handler ()  {//  processing public after receiving message  void handlemessage (message msg)  {switch  (msg.what)  {case 1: Mtextview.settext (" +  (i++)); Super.handlemessage (msg);}}; Class buttonlistener implements onclicklistener, ontouchlistener {public void  onclick (view v)  {if  (V.getid ()  == r.id.button)  {log.d ("Test",  "cansal  button ---> click ");}}   You need to replicate the Ontouch event, you need to open a thread  sleep 1 seconds   Perform an additional operation, the update data needs to be placed inside the UI thread. &nBSP;   can jump out of thread Breaktimer timer;public boolean ontouch (view v, motionevent  event)  {switch  (Event.getaction ())  {case MotionEvent.ACTION_DOWN:timer =  New timer (TRUE); System.out.println ("++++++key_down"); Timer.schedule (New mytask (),  1000, 1000);break;case  MotionEvent.ACTION_UP:System.out.println ("++++++key_up"); Timer.cancel (); break;case motionevent.action_ CANCEL:System.out.println ("++++++key_cancel"); Timer.cancel (); break;} Return true;}}}


Reference article:

    1. Http://www.cnblogs.com/xzf158/archive/2009/09/04/1560042.html

    2. http://alex-yang-xiansoftware-com.iteye.com/blog/1958905

    3. Http://www.android100.org/html/201409/12/64378.html

    4. Http://www.linuxidc.com/Linux/2011-10/45976.htm


This article is from the information security-database security blog, so be sure to keep this source http://fergusj.blog.51cto.com/8835051/1687825

Android, press and hold button to increase continuously

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.