Actvity中TextView的Touch事件

举报
ShaderJoy 发表于 2021/12/30 00:57:52 2021/12/30
【摘要】 package com.domo.touch; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; i...


  
  1. package com.domo.touch;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;
  8. import android.widget.TextView;
  9. public class TestTouchActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. TextView textView = (TextView) findViewById(R.id.textview);
  16. textView.setOnTouchListener(new OnTouchListener() {
  17. public boolean onTouch(View v, MotionEvent event) {
  18. // TODO Auto-generated method stub
  19. switch (event.getAction()) {
  20. case MotionEvent.ACTION_DOWN:
  21. Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  22. break;
  23. case MotionEvent.ACTION_UP:
  24. Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
  25. break;
  26. default:
  27. break;
  28. }
  29. return false;//注意:return false
  30. }
  31. });
  32. }
  33. @Override
  34. public boolean dispatchTouchEvent(MotionEvent ev) {
  35. // TODO Auto-generated method stub
  36. switch (ev.getAction()) {
  37. case MotionEvent.ACTION_DOWN:
  38. Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  39. break;
  40. case MotionEvent.ACTION_UP:
  41. Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
  42. break;
  43. default:
  44. break;
  45. }
  46. Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
  47. return super.dispatchTouchEvent(ev);//注意
  48. }
  49. }
代码图如上:
dispatchTouchEvent的返回值为super.dispatchTouchEvent(ev)时,获取到super.dispatchTouchEvent(ev)为False;点击Textview的显示的结果为

04-28 01:09:32.978: V/TAG(5733): Activity_dis: ACTION_DOWN0
 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0
 04-28 01:09:32.978: V/TAG(5733): Activity_dis: false
 04-28 01:09:32.978: V/TAG(5733): TextView: ACTION_DOWN0     //TextView只能获取到Down事件
 04-28 01:09:33.068: V/TAG(5733): Activity_dis: false
 04-28 01:09:33.088: V/TAG(5733): Activity_dis: ACTION_UP1
 04-28 01:09:33.088: V/TAG(5733): Activity_dis: false
dispatchTouchEvent false


  
  1. public boolean dispatchTouchEvent(MotionEvent ev) {
  2. // TODO Auto-generated method stub
  3. switch (ev.getAction()) {
  4. case MotionEvent.ACTION_DOWN:
  5. Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  6. break;
  7. case MotionEvent.ACTION_UP:
  8. Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
  9. break;
  10. default:
  11. break;
  12. }
  13. Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
  14. return false; //即这里改为False时
  15. }
1 04-28 01:14:25.467: V/TAG(5884): Activity_dis: ACTION_DOWN0
2 04-28 01:14:25.467: V/TAG(5884): TextView: ACTION_DOWN0         //只能获取到一次Down事件
3 04-28 01:14:25.467: V/TAG(5884): Activity_dis: false
4 04-28 01:14:25.571: V/TAG(5884): Activity_dis: ACTION_UP1
5 04-28 01:14:25.571: V/TAG(5884): Activity_dis: false
dispatchTouchEvent true
1 04-28 01:17:00.398: V/TAG(5995): Activity_dis: ACTION_DOWN0
2 04-28 01:17:00.398: V/TAG(5995): TextView: ACTION_DOWN0               // 效果没见发生变化
3 04-28 01:17:00.398: V/TAG(5995): Activity_dis: false
4 04-28 01:17:00.418: V/TAG(5995): Activity_dis: ACTION_UP1
5 04-28 01:17:00.418: V/TAG(5995): Activity_dis: false
Activity onThouchEvent事件


  
  1. package com.domo.touch;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;
  8. import android.widget.TextView;
  9. public class TestTouchActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. TextView textView = (TextView) findViewById(R.id.textview);
  16. textView.setOnTouchListener(new OnTouchListener() {
  17. public boolean onTouch(View v, MotionEvent event) {
  18. // TODO Auto-generated method stub
  19. switch (event.getAction()) {
  20. case MotionEvent.ACTION_DOWN:
  21. Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  22. break;
  23. case MotionEvent.ACTION_UP:
  24. Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
  25. break;
  26. default:
  27. break;
  28. }
  29. return false;
  30. }
  31. });
  32. }
  33. @Override
  34. public boolean dispatchTouchEvent(MotionEvent ev) {
  35. // TODO Auto-generated method stub
  36. switch (ev.getAction()) {
  37. case MotionEvent.ACTION_DOWN:
  38. Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  39. break;
  40. case MotionEvent.ACTION_UP:
  41. Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
  42. break;
  43. default:
  44. break;
  45. }
  46. Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
  47. return true;//注意
  48. }
  49. @Override
  50. public boolean onTouchEvent(MotionEvent event) {//在Activity中添加了onTouchEvent事件
  51. // TODO Auto-generated method stub
  52. switch (event.getAction()) {
  53. case MotionEvent.ACTION_DOWN:
  54. Log.v("TAG", "onTouchEvent: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  55. break;
  56. case MotionEvent.ACTION_UP:
  57. Log.v("TAG", "onTouchEvent: ACTION_UP" + MotionEvent.ACTION_UP);
  58. break;
  59. default:
  60. break;
  61. }
  62. Log.v("TAG", "onTouchEvent: " + super.onTouchEvent(event));
  63. return super.onTouchEvent(event);//注意
  64. }
  65. }
点击Textview结果:

04-28 01:22:11.957: V/TAG(6152): Activity_dis: ACTION_DOWN0
04-28 01:22:11.957: V/TAG(6152): TextView: ACTION_DOWN0       //还是只有一次
04-28 01:22:11.957: V/TAG(6152): onTouchEvent: ACTION_DOWN0          
04-28 01:22:11.957: V/TAG(6152): onTouchEvent: false
04-28 01:22:11.957: V/TAG(6152): Activity_dis: false
04-28 01:22:12.047: V/TAG(6152): Activity_dis: ACTION_UP1
04-28 01:22:12.047: V/TAG(6152): onTouchEvent: ACTION_UP1
04-28 01:22:12.058: V/TAG(6152): onTouchEvent: false
04-28 01:22:12.058: V/TAG(6152): Activity_dis: false
super.onTouchEvent(event) return false


04-28 01:25:17.378: V/TAG(6152): Activity_dis: ACTION_DOWN0
04-28 01:25:17.378: V/TAG(6152): TextView: ACTION_DOWN0          //依然只有一次
04-28 01:25:17.378: V/TAG(6152): onTouchEvent: ACTION_DOWN0
04-28 01:25:17.378: V/TAG(6152): onTouchEvent: false
04-28 01:25:17.378: V/TAG(6152): Activity_dis: false
04-28 01:25:17.468: V/TAG(6152): Activity_dis: ACTION_UP1
04-28 01:25:17.468: V/TAG(6152): onTouchEvent: ACTION_UP1
04-28 01:25:17.468: V/TAG(6152): onTouchEvent: false
04-28 01:25:17.468: V/TAG(6152): Activity_dis: false
再把public boolean dispatchTouchEvent(MotionEvent ev) { 也返回 return false;

04-28 01:27:16.178: V/TAG(6304): Activity_dis: ACTION_DOWN0
04-28 01:27:16.178: V/TAG(6304): TextView: ACTION_DOWN0          //依然只有一次

04-28 01:27:16.178: V/TAG(6304): onTouchEvent: ACTION_DOWN0
04-28 01:27:16.178: V/TAG(6304): onTouchEvent: false
04-28 01:27:16.178: V/TAG(6304): Activity_dis: false
04-28 01:27:16.298: V/TAG(6304): Activity_dis: ACTION_UP1
04-28 01:27:16.298: V/TAG(6304): onTouchEvent: ACTION_UP1
04-28 01:27:16.298: V/TAG(6304): onTouchEvent: false
04-28 01:27:16.298: V/TAG(6304): Activity_dis: false

  
  1. textView.setOnTouchListener(new OnTouchListener() {
  2. public boolean onTouch(View v, MotionEvent event) {
  3. // TODO Auto-generated method stub
  4. switch (event.getAction()) {
  5. case MotionEvent.ACTION_DOWN:
  6. Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
  7. break;
  8. case MotionEvent.ACTION_UP:
  9. Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
  10. break;
  11. default:
  12. break;
  13. }
  14. return true; //注意
Textview才能监听到 TextView: ACTION_DOWN0及TextView : ACTION_UP1的动作。
04-28 01:36:26.057: V/TAG(6628): Activity_dis: ACTION_DOWN0
04-28 01:36:26.057: V/TAG(6628): TextView: ACTION_DOWN0
04-28 01:36:26.057: V/TAG(6628): Activity_dis: true
04-28 01:36:26.148: V/TAG(6628): Activity_dis: ACTION_UP1
04-28 01:36:26.148: V/TAG(6628): TextView : ACTION_UP1//监听成功
04-28 01:36:26.148: V/TAG(6628): Activity_dis: true
3.png




文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/8742633

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。