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:
-
Http://www.cnblogs.com/xzf158/archive/2009/09/04/1560042.html
-
http://alex-yang-xiansoftware-com.iteye.com/blog/1958905
-
Http://www.android100.org/html/201409/12/64378.html
-
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