package com.example.k.touchtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button; float valueX = 0; float X = 0; float valueY = 0; float Y = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.i("ok", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>Click()"); } }); button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { Log.i("ok", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>onTouch() "+ motionEvent.getAction()); return false; } }); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.i("ok", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>dispatchTouchEvent()"); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: valueX = ev.getX(); valueY = ev.getY(); break; case MotionEvent.ACTION_UP: valueX = 0f; X = 0; valueY = 0f; Y = 0; break; case MotionEvent.ACTION_MOVE: Log.i("ok","MOVE---------------"); if(valueX>ev.getX()){ X=valueX-ev.getX(); }else{ X = ev.getX()-valueX; } if(valueY>ev.getY()){ Y=valueY-ev.getY(); }else{ Y = ev.getY()-valueY; } if(X>Y){ //Log.i("ok","-------------X"); }else{ //Log.i("ok","-------------Y"); } // Log.i("ok","-------------Y事件"); break; default: break; } boolean k = super.dispatchTouchEvent(ev); Log.i("ok","k = "+k); return k; // return true; } }
解决事件冲突的代码参考
最新推荐文章于 2023-04-16 16:44:18 发布