android MotionEvent 获取长按压时间长

思路:

1.记录ACTION_DOWN的aX, aY坐标;

2.在ACTION_MOVE判断是否移动,移动则取消记录时间,没移动就记录;

3.记录时间,按下坐标,移动坐标分别显示在TextView aa, bb, cc;

//声明

1 public class MainActivity extends ActionBarActivity {
2     private static TextView aa, bb, cc;
3     private float atime;
4     private float aX, aY;
5     private boolean mPressBreak = false;

//指定

1 aa = (TextView) findViewById(R.id.textView1);
2 bb = (TextView) findViewById(R.id.textView2); 
3 cc = (TextView) findViewById(R.id.textView3);

 

//获取按压时间长

 1 @Override
 2     public boolean onTouchEvent(MotionEvent event) {
 3         // TODO Auto-generated method stub
 4         super.onTouchEvent(event);
 5         String str = "";
 6         switch (event.getAction()) {
 7         case MotionEvent.ACTION_DOWN:
 8             aX = event.getX();
 9             aY = event.getY();
10             str = String.valueOf(aX) + " , " + String.valueOf(aY);
11             bb.setText(str);
12             mPressBreak = false;
13             break;
14         case MotionEvent.ACTION_MOVE:
15 
16             atime = (event.getEventTime() - event.getDownTime()) / 1000;
17             str = String.valueOf(event.getX()) + " , "
18                     + String.valueOf(event.getY());
19             if ((Math.abs((event.getX() - aX)) > 10)
20                     || (Math.abs(event.getY() - aY) > 10)) {
21                 atime = 0;
22                 mPressBreak = true;
23             }
24             break;
25 
26         }
27 
28         if (!mPressBreak) {
29             aa.setText(String.valueOf((int) (atime)));
30             cc.setText(str);
31         }
32 
33         return true;
34     }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。