我刚接触到android,我遇到了问题。 我试图创建一个应用程序,按住按钮,计时器启动。如果你松开按下按钮,定时器会自动重置。如果你设法保持到定时器达到零,它将发出声音。我用一些丑陋的代码来管理点击按钮,它启动了一个计时器,但是我无法实现“OnTouch”列表器。
如何在此代码中将OnCLick更改为OnTouch?
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private KarinCountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private final long startTime = 900000; //15 min 1800000
private final long interval = 1000;
SoundManager mymanager; //sound
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new KarinCountDownTimer(startTime,interval);
text.setText(text.getText() + String.valueOf(startTime));
mymanager = new SoundManager(this);
}
@Override
public void onClick(View v)
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESET");
}
}
// CountDownTimer class
public class KarinCountDownTimer extends CountDownTimer
{
public KarinCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's up!");
mymanager.playSound(R.raw.zen_sound1);
}
@Override
public void onTick(long millisUntilFinished)
{
text.setText("" + millisUntilFinished);
}
}
protected void onPause(){
mymanager.stopSound();
super.onPause();
}
protected void onDestroy(){
mymanager.release();
super.onDestroy();
}
}