Android 安卓:按钮跟随光标
如何在光标移动时更改按钮的位置。Android 安卓:按钮跟随光标,android,button,Android,Button,如何在光标移动时更改按钮的位置。 我的意思是如何在我移动按钮时让按钮跟随光标。尝试实现触控监听器。卡拉宾:这是对的:'codemSwitchBtn=Buttoninflater.inflateR.layout.status\u bar,null.findviewbydr.id.lock\u开关\u button;mSwitchBtn.setOnTouchListenernew View.OnTouchListener{private int x;private int y;public bool
我的意思是如何在我移动按钮时让按钮跟随光标。尝试实现触控监听器。卡拉宾:这是对的:'codemSwitchBtn=Buttoninflater.inflateR.layout.status\u bar,null.findviewbydr.id.lock\u开关\u button;mSwitchBtn.setOnTouchListenernew View.OnTouchListener{private int x;private int y;public boolean onTouchView v,MotionEvent事件{switchevent.getAction{case MotionEvent.ACTION_DOWN:x=intevent.getX;y=intevent.getY;break;case MotionEvent.ACTION_MOVE:mSwitchBtn.layoutx,y,mSwitchBtn.getWidth+x,mSwitchBtn.getHeight+y;mSwitchBtn.invalidate;}返回真;};code'@这似乎是对的,但我不确定mSwitchBtn.getHeight和mSwitchBtn.getWidth方法-它可以返回0。您可以使用`作为一行代码,使用\发布预格式化文本 new View.OnTouchListener() { private float downX = 0; private float downY = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case (MotionEvent.ACTION_DOWN):
this.downX = motionEvent.getX();
this.downY = motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
final View btn= layout.findViewById(R.id.button);
currentView.layout(newLeft,
newTop, newRight,
newBottom));
break;
}
return true;
}
}