Android-sliding events of controls

Source: Internet
Author: User

In android development, we often use a very convenient effect, that is, to switch between the left and right sides or slide up and down the content or control, for example, to delete a piece of information, sliding to the left will display a delete function key to delete.

To implement this function, we can use the onTouch event for processing. The specific code is as follows:

Package com. example. text; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. textView; public class MainActivity extends Activity {private TextView txtTest; private float moveX; private float moveY; private float pressX; private float pressY; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); txtTest = (textview1_findviewbyid(r.id.txt Test); txtTest. setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {// press case MotionEvent. ACTION_DOWN: pressX = event. getX (); pressY = event. getY (); break; // move case MotionEvent. ACTION_MOVE: moveX = event. getX (); moveY = event. getY (); break; // release case MotionEvent. ACTION_UP: if (moveX-pressX> 0 & Math. abs (moveY-pressY) <50) {Log. I ("message", "right");} else if (moveX-pressX <0 & Math. abs (moveY-pressY) <50) {Log. I ("message", "Left") ;}break; default: break ;}return true ;}});}}

 

 

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.