Actvity中TextView的Touch事件
【摘要】
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...
-
package com.domo.touch;
-
-
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 TestTouchActivity extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
TextView textView = (TextView) findViewById(R.id.textview);
-
textView.setOnTouchListener(new OnTouchListener() {
-
-
public boolean onTouch(View v, MotionEvent event) {
-
// TODO Auto-generated method stub
-
switch (event.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
-
return false;//注意:return false
-
}
-
});
-
}
-
-
@Override
-
public boolean dispatchTouchEvent(MotionEvent ev) {
-
// TODO Auto-generated method stub
-
switch (ev.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
-
return super.dispatchTouchEvent(ev);//注意
-
}
-
-
}
代码图如上:当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
-
public boolean dispatchTouchEvent(MotionEvent ev) {
-
// TODO Auto-generated method stub
-
switch (ev.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
-
return false; //即这里改为False时
-
}
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事件
-
package com.domo.touch;
-
-
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 TestTouchActivity extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
TextView textView = (TextView) findViewById(R.id.textview);
-
textView.setOnTouchListener(new OnTouchListener() {
-
-
public boolean onTouch(View v, MotionEvent event) {
-
// TODO Auto-generated method stub
-
switch (event.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
-
return false;
-
}
-
});
-
}
-
-
@Override
-
public boolean dispatchTouchEvent(MotionEvent ev) {
-
// TODO Auto-generated method stub
-
switch (ev.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "Activity_dis: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "Activity_dis: ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
Log.v("TAG", "Activity_dis: " + super.dispatchTouchEvent(ev));
-
return true;//注意
-
}
-
-
@Override
-
public boolean onTouchEvent(MotionEvent event) {//在Activity中添加了onTouchEvent事件
-
// TODO Auto-generated method stub
-
switch (event.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "onTouchEvent: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "onTouchEvent: ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
Log.v("TAG", "onTouchEvent: " + super.onTouchEvent(event));
-
return super.onTouchEvent(event);//注意
-
}
-
-
}
点击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
-
textView.setOnTouchListener(new OnTouchListener() {
-
-
public boolean onTouch(View v, MotionEvent event) {
-
// TODO Auto-generated method stub
-
switch (event.getAction()) {
-
case MotionEvent.ACTION_DOWN:
-
Log.v("TAG", "TextView: ACTION_DOWN" + MotionEvent.ACTION_DOWN);
-
break;
-
case MotionEvent.ACTION_UP:
-
Log.v("TAG", "TextView : ACTION_UP" + MotionEvent.ACTION_UP);
-
break;
-
default:
-
break;
-
}
-
-
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
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8742633
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)