Android Custom button for long press function
by customizing the button, write a longtouchbtn class, execute the Ontouchevent event at the time of the press, and use the callback function to implement the long press function with this event!
Xml:
<HUAHUA.BTNLONGTOUCH.LONGTOUCHBTN android:id= "@+id/btn2" android:layout_width= "Fill_parent " android:layout_height= "Wrap_content" android:text= "Custom btn"/> <textview android:id= "@+id/tv1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "0" / >
Activity
public class Mainactivity extends Activity {private TextView tv1;private longtouchbtn btn1;private int num=0; @Overrideprotected void O Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tv1 = (TextView) Findviewbyid (R.ID.TV1); BTN1 = (longtouchbtn) Findviewbyid (R.ID.BTN2); Btn1.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {log.i ("Huahua", " Custom button handling click ");}); Btn1.setonlongclicklistener (New View.onlongclicklistener () {@Overridepublic Boolean onlongclick (View v) {log.i (" Huahua "," custom button handles long press the corresponding "); return false;}); * * This is a custom interface specifically responsible for handling long press logic * @param listener * listener. * @param time * The 2nd parameter passed in 1000, representing 1 seconds to process the Onlongtouch () method */btn1.setonlongtouchlistener (new Longtouchlistener () {@Ov erridepublic void Onlongtouch () {num++; Tv1.settext (num+ ""); LOG.I ("Huahua", "long Press");}},1000)}}
HUAHUA.BTNLONGTOUCH.LONGTOUCHBTN:
public class LONGTOUCHBTN extends button{/** * Records the current custom btn whether to press */private boolean clickdown = false;/** * drop-down refreshed callback interface */privat E longtouchlistener mlistener;/** * button long time interval how many milliseconds to process the callback method */private int mtime;/** * constructor * @param context * @param attrs * /public longtouchbtn (Context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor stub }/** * Process Touch Event */@Overridepublic Boolean ontouchevent (Motionevent event) {if (event.getaction () = = Motionevent.action_ Down) {Clickdown = True;new longtouchtask (). Execute (); LOG.I ("Huahua", "Press");} else if (event.getaction () = = motionevent.action_up) {Clickdown = false; LOG.I ("Huahua", "Bounce");} Return Super.ontouchevent (event);} /** * Causes the current thread to sleep for a specified number of milliseconds. * * @param time * Specifies how long the current thread sleeps, in milliseconds */private void sleep (int time) {try {thread.sleep (time);} catch (Interrupt Edexception e) {e.printstacktrace ();}} /** * Handles long press tasks */class Longtouchtask extends Asynctask<void, Integer, void>{@Overrideprotected Void doinbackground ( Void ... paraMS) {while (Clickdown) {sleep (mtime);p ublishprogress (0);} return null;} @Overrideprotected void OnPostExecute (void result) {} @Overrideprotected void Onprogressupdate (Integer ... values) { Mlistener.onlongtouch ();}} /** * Give long press BTN control to register a listener. * * @param listener * listener implementation. * @param time * How many milliseconds interval to process the callback method */public void Setonlongtouchlistener (Longtouchlistener Listener, int.) { Mlistener = Listener;mtime = time;} /** * Long press the Listener interface, where the button is long pressed, the listener should be registered to obtain the callback. */public interface Longtouchlistener {/** * Handles long press callback method */void Onlongtouch ();}}