imageview的ontouch、button的ontouch和activity的ontouchevent

浙外 2017-08-18 01:21:59
前提:
  在imageview上设置ontouchlistener,然后返回false;
  在button上设置ontouchlistener,然后也返回false;
  重写activicy的ontouchevent,最后返回true;
现象:
  点击了imageview,执行了事件代码,同时因为返回false,所以继续会执行activity上的ontouchevent中的代码;
  点击了button,执行了事件代码,但是同样返回false,后续并没有执行activity上的ontouchevent中的代码
为什么?
...全文
141 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
江三木洛 2017-08-18
  • 打赏
  • 举报
回复
执行完ontouchlistener后,如果返回false的话会继续去执行onTouchEvent里的方法,button的onTouchEvent会默认返回true,你没有消费触摸事件,但是button自己会消费 如果想执行activity的ontouchevent方法,那需要重写Button的onTouchEvent方法,也返回false
可以滑动的界面切换tabhost package com.caigang.test; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; public class MainActivity extends TabActivity implements OnTabChangeListener,OnGestureListener { private GestureDetector gestureDetector; private FrameLayout frameLayout; private CustomTabHost tabHost; private TabWidget tabWidget; private static final int FLEEP_DISTANCE = 120; /** 记录当前分页ID */ private int currentTabID = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabHost = (CustomTabHost) findViewById(android.R.id.tabhost); tabWidget = (TabWidget) findViewById(android.R.id.tabs); tabHost.setOnTabChangedListener(this); init(); gestureDetector = new GestureDetector(this); new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }; frameLayout = tabHost.getTabContentView(); } private void init() { setIndicator(R.drawable.icon1, 0, new Intent(this,TabActivity01.class)); setIndicator(R.drawable.icon2, 1, new Intent(this,TabActivity02.class)); setIndicator(R.drawable.icon3, 2, new Intent(this,TabActivity03.class)); setIndicator(R.drawable.icon4, 3, new Intent(this,TabActivity04.class)); } private void setIndicator(int icon, int tabId, Intent intent) { View localView = LayoutInflater.from(this.tabHost.getContext()).inflate(R.layout.tab, null); ((ImageView) localView.findViewById(R.id.tab_image)).setBackgroundResource(icon); String str = String.valueOf(tabId); TabHost.TabSpec localTabSpec = tabHost.newTabSpec(str).setIndicator(localView).setContent(intent); tabHost.addTab(localTabSpec); } @Override public void onTabChanged(String tabId) { //tabId值为要切换到的tab页的索引位置 int tabID = Integer.valueOf(tabId); for (int i = 0; i < tabWidget.getChildCount(); i++) { if (i == tabID) { tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.bule); } else { tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.white); } } } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { if (e1.getX() - e2.getX() <= (-FLEEP_DISTANCE)) {//从左向右滑动 currentTabID = tabHost.getCurrentTab() - 1; if (currentTabID < 0) { currentTabID = tabHost.getTabCount() - 1; } } else if (e1.getX() - e2.getX() >= FLEEP_DISTANCE) {//从右向左滑动 currentTabID = tabHost.getCurrentTab() + 1; if (currentTabID >= tabHost.getTabCount()) { currentTabID = 0; } } tabHost.setCurrentTab(currentTabID); return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧