gpt4 book ai didi

java - 当未满足要求时,程序正在进入 switch case

转载 作者:行者123 更新时间:2023-12-02 05:36:13 25 4
gpt4 key购买 nike

所以我创建了一个使用两个操纵杆的程序。每个操纵杆都处于相对布局中,操纵杆的拇指被困在布局内,并返回我用作 X 和 Y 值的边距值。这是我的监听器程序:

public boolean onTouch(View view, MotionEvent event) {
final int action = event.getAction();
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (action & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
}

case MotionEvent.ACTION_MOVE:{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
leftM = X - _xDelta;
//System.out.println("LeftM: " + leftM);
topM = Y - _yDelta;
//System.out.println("TopM: " + topM);
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;

if(leftM < 0) {

leftM = 0;

} else if (leftM > 220) {

leftM= 220;

}

if(topM < 0) {

topM = 0;

} else if (topM > 220) {

topM = 220;

}

//Quadrant I
if (topM < 110 && leftM > 110 ){

x = (int) (leftM - 110);
y = (int) (110 - topM);

//Quadrant II
} else if (topM < 110 && leftM < 110) {

x = (int) (leftM - 110);
y = (int) (110 - topM);

//Quadrant III
} else if ( topM > 110 && leftM < 110) {

x = (int) (leftM - 110);
y = (int) -(topM - 110);

//Quadrant IV
} else if (topM > 110 && leftM > 110){

x = (int) (leftM - 110);
y = (int) -(topM - 110);

//Origin
} else {

layoutParams.leftMargin = leftM;
layoutParams.topMargin = topM;

}

if ((Math.pow(x, 2) + Math.pow(y, 2)) <= 12100) {

recentX = leftM;
recentY = topM;
layoutParams.topMargin = topM;
layoutParams.leftMargin = leftM;


} else{

layoutParams.leftMargin = recentX;
layoutParams.topMargin = recentY;

}

switch (view.getId()) {

case R.id.thumbL:
view.setLayoutParams(layoutParams);
joystick1.LeftY = (byte) (layoutParams.topMargin + 37);
joystick1.LeftX = (byte) (layoutParams.leftMargin + 37);
System.out.println("Left X Value: " + layoutParams.leftMargin + 37);
System.out.println("Left Y Value: " + layoutParams.topMargin + 37);

case R.id.thumbR:
view.setLayoutParams(layoutParams);
joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
}
break;

我必须将象限重新映射到普通坐标平面(这是 if 语句的要点),然后我就有了 switch 语句。我只希望每个案例在相应的拇指被按下时运行。然而,如果我不必握住右手拇指,它会打印“Right X Value”,显示它已进入该情况。有什么想法吗?

最佳答案

你可以使用break。

        switch (view.getId()) {

case R.id.thumbL:
view.setLayoutParams(layoutParams);
joystick1.LeftY = (byte) (layoutParams.topMargin + 37);
joystick1.LeftX = (byte) (layoutParams.leftMargin + 37);
System.out.println("Left X Value: " + layoutParams.leftMargin + 37);
System.out.println("Left Y Value: " + layoutParams.topMargin + 37);
**break;**

case R.id.thumbR:
view.setLayoutParams(layoutParams);
joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
**break;**
}

关于java - 当未满足要求时,程序正在进入 switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24941832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com