ボタンを押すと値がラッチされ、もう一度押すとラッチが解除されるボタンを作成する方法を理解しようとしています。
これに対する私の方法は次のとおりです。
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;
}
次のように、flipFlop メソッドが onTouch の下で呼び出されます。
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;
}
1 回クリックすると、状態は false に設定され、変化しません。ダブルクリックすると、2 つの状態が切り替わります。何故ですか?
また、状態を保持する配列で作成しようとすると、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];
}