我的onTouchlistener功能之一出了问题。 这是我的代码。
private float mPrevX;
private float mPrevY;
private static final String TAG = "moveAbleButton";
public MainActivity mainActivity;
public moveAbleButton(MainActivity mainActivity1)
{
mainActivity=mainActivity1;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float currX,currY;
if (motionEvent.getActionMasked()==MotionEvent.ACTION_BUTTON_PRESS)
{
Log.d(TAG, "onTouch: ACTION_BUTTON_PRESS");
Intent i = new Intent(mainActivity, newActivity.class);
mainActivity.startActivity(i);
return true;
}
else if(motionEvent.getActionMasked()==MotionEvent.ACTION_DOWN)
{
Log.d(TAG, "onTouch:ACTION_DOWN ");
mPrevX = motionEvent.getX();
mPrevY = motionEvent.getY();
return true;
}
else if (motionEvent.getActionMasked()==MotionEvent.ACTION_MOVE)
{
Log.d(TAG, "onTouch: ACTION_MOVE");
currX = motionEvent.getRawX();
currY = motionEvent.getRawY();
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0);
android.support.design.widget.CoordinatorLayout.LayoutParams layoutParams = new android.support.design.widget.CoordinatorLayout.LayoutParams(marginParams);
view.setLayoutParams(layoutParams);
return true;
}
return false;
}
你可以看到我想在屏幕上的任何地方移动我的按钮(它工作正常)但我也想当我按下按钮我的新活动也叫。但是motionEvent.Action_Button_press 从不打电话。 我的活动中还有三栏的网格视图抱歉英语不好,并提前致谢。
和我的主要活动
Button fab = (Button) findViewById(R.id.fab);
moveAbleButton buttonMove = new moveAbleButton(this);
fab.setOnTouchListener(buttonMove);