1

我是 Android 新手,我遇到了问题。我正在尝试创建一个应用程序,当您按住按钮时,计时器就会启动。如果您松开按下的按钮,计时器会自行重置。如果您设法按住直到计时器归零,它将播放声音。我用一些丑陋的代码管理了可启动计时器的可点击按钮,但我无法实现OnTouch监听器。

如何 在此代码中将更改OnCLick为 a ?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();
    }
    }
4

0 回答 0