在我的代码中,onLongPress全屏工作,但我需要onLongPress进入特定的相对布局。在LongPress中获取X和Y值后,我必须在onLongPress事件中执行一些操作。
public class MainActivity extends ActionBarActivity {
private GestureDetectorCompat mDetector;
public Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myView);
}
@Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public void message(Context ctx,String msg,int duration) {
Toast.makeText(ctx, msg, duration).show();
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public void onLongPress(MotionEvent e) {
float x = e.getX();
float y = e.getY();
message(getBaseContext()," X value is " + x + " Y value is "+ y ,Toast.LENGTH_SHORT);
super.onLongPress(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
message(getBaseContext(),"Single tab" ,Toast.LENGTH_SHORT);
return super.onSingleTapConfirmed(e);
}
}
}
答案 0 :(得分:0)
尝试使用处理程序处理事件
imgMainNotification.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new Handler().postDelayed(new Runnable()
{
public void run()
{
//do what youwanna do on long press here
}
}, 1 * 3000);
return true;
case MotionEvent.ACTION_UP:
return true;
default:
return false;
}
}
});