0
當我比較duration2
和duration4
的值時,我正在使用的Toast消息將打印兩次,但答案/消息是正確的。我不知道爲什麼會發生。請告訴我如何解決這個問題。爲什麼吐司消息要打印兩次?
Button b1, b2, b3;
long duration, duration2, duration3, duration4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
duration=System.currentTimeMillis();
}
else if (event.getAction()==MotionEvent.ACTION_UP) {
duration2= System.currentTimeMillis()-duration;
}
return true;
}
});
b2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
duration3=System.currentTimeMillis();
}
else if (event.getAction()==MotionEvent.ACTION_UP) {
duration4= System.currentTimeMillis()-duration3;
}
if(duration4<duration2) {
Toast.makeText(getApplicationContext(), "Good", Toast.LENGTH_SHORT).show();
}
else if(duration4>duration2) {
Toast.makeText(getApplicationContext(), "Too Slow Man", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
香港樂施會,會嘗試這感謝。 –