Comparison of Android custom button click event and long press event
A button implements click and long press events at the same time, and sometimes there will be conflicts. We customize buttons to distinguish click and long press events based on this phenomenon.
middle
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="" android:orientation="vertical" > < android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Custom Btn" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> <SeekBar android: android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> </LinearLayout>
middle
public class MainActivity extends Activity { private TextView Tv1; private LongTouchBtn Btn1; private int num=0; private SeekBar sbar; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); sbar= (SeekBar) findViewById(); Tv1 = (TextView)findViewById(.tv1); Btn1 = (LongTouchBtn)findViewById(.btn2); (new () { @Override public void onClick(View arg0) { ("huahua", "Custom Button Processing Clicks"); } }); (new () { @Override public boolean onLongClick(View v) { ("huahua", "Custom button processing, long press and press"); return true; } }); /** * This is a custom interface that is specifically responsible for handling long-press logic * @param listener * Listener. * @param time * The second parameter is passed to 1000, indicating that the onLongTouch() method is processed once in 1 second */ (new LongTouchListener() { @Override public void onLongTouch() { num++; int seekbar_progress = (); ("huahua", "seekbar_progress="+seekbar_progress); seekbar_progress++; (seekbar_progress); (num+""); ("huahua", "Long-press"); } },1000); } }
3. Create a new custom LongTouchBtn class
public class LongTouchBtn extends Button{ /** * Record whether the current custom Btn is pressed */ private boolean clickdown = false; /** * Pull down to refresh the callback interface */ private LongTouchListener mListener; /** * How many milliseconds are used to handle the button? Callback method */ private int mtime; /** * Constructor * @param context * @param attrs */ public LongTouchBtn(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } /** * Handle touch events */ @Override public boolean onTouchEvent(MotionEvent event) { if(() == MotionEvent.ACTION_DOWN) { clickdown = true; new LongTouchTask().execute(); ("huahua", "Press"); } else if(() == MotionEvent.ACTION_UP) { clickdown = false; ("huahua", "Boom up"); } return (event); } /** * Make the current thread sleep the specified number of milliseconds. * * @param time * Specify how long the current thread sleeps, in milliseconds */ private void sleep(int time) { try { (time); } catch (InterruptedException e) { (); } } /** * Handle long press tasks */ class LongTouchTask extends AsyncTask<Void, Integer, Void>{ @Override protected Void doInBackground(Void... params) { while(clickdown) { sleep(mtime); publishProgress(0); } return null; } @Override protected void onPostExecute(Void result) { } @Override protected void onProgressUpdate(Integer... values) { (); } } /** * Register a listener for the long press btn control. * * @param listener * The listener implementation. * @param time * How many milliseconds intervals are used to handle callback methods */ public void setOnLongTouchListener(LongTouchListener listener, int time) { mListener = listener; mtime = time; } /** * Press and hold the monitor interface long, and the place where you use the button to hold the button should register this listener to get the callback. */ public interface LongTouchListener { /** * Method of handling long press callback */ void onLongTouch(); } }
Thank you for reading, I hope it can help you. Thank you for your support for this site!