我正在尝试了解如何在按下按钮时按下某个按钮,当按下该按钮时,它会解锁。
我的方法如下:
boolean r1 = false;
boolean r2 = true;
private boolean flipFlop(boolean read, int i)
{
if(read == true)
{
if (r1 == true && r2 == false)
{
r1 = false;
r2 = true;
}
else if(r1 == false && r2 == true)
{
r1 = true;
r2 = false;
}
}
return r1;
}
在onTouch下调用flipFlop方法,如下所示:
public boolean onTouch(View view, MotionEvent motion)
{
boolean pressCheck = false;
switch(motion.getAction())
{
case MotionEvent.ACTION_UP:
{
pressCheck = flipFlop(view.isPressed(), 1);
textView.setText("State is: " + pressCheck);
}
case MotionEvent.ACTION_DOWN:
{
pressCheck = flipFlop(view.isPressed(), 1);
textView.setText("State is: " + pressCheck);
}
}
return false;
}
单击一次时,状态将设置为false并且不会更改。 双击时,它会在两种状态之间翻转。那是为什么?
此外,当我尝试使用数组来保存状态时,它会锁定为true并且在双击时不会更改:
private boolean[][] latch = {{false, false, false}, {true, true, true}};
public boolean flipFlop(boolean read, int i)
{
if(read == true)
{
if(latch[i][2] == true && latch[i][1] == false)
{
latch[i][1] = true;
latch[i][2] = false;
}
else if(latch[i][2] == false && latch[i][1] == true)
{
latch[i][1] = false;
latch[i][2] = true;
}
}
return latch[i][1];
}