我刚刚开始使用Android Studio,并且想通过Button Clickevent创建一个具有可设置的Start值的CountDownTimer。
到目前为止的代码:

private static final String TAG = "MSchlanstedt";
private int number;
boolean status = true;
private CountDownTimer myTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ProgressBar bar = (ProgressBar)findViewById(R.id.progressBar);
    final EditText Time = (EditText)findViewById(R.id.text);
    final Button Button = (Button)findViewById(R.id.start);
    final TextView textView = (TextView)findViewById(R.id.textView);

    Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (status == true)
            {
                Button.setText("Stop");
                number = Integer.valueOf(Time.getText().toString())*1000;
                myTimer.start();
                status = false;
                Log.d(TAG, "Button.onClick():Start " + number);
            }else
            {
                myTimer.cancel();
                Button.setText("Start");
                Log.d(TAG, "Button.onClick():Stop " + number);
                status = true;
            }

        }
    });

    myTimer = new CountDownTimer(number,1000) {
        public void onTick(long millisUntilFinished)
        {
            Log.v(TAG,"CountDownTimer.onTick(): sUntilFinished: "+number+"/"+ millisUntilFinished /1000);
            textView.setText(String.valueOf(millisUntilFinished/1000));
        }
        public void onFinish()
        {
            Log.v(TAG,"CountDownTimer.onFinish()");
        }
    }.start();
}


...`

我的问题是Button在Text Start和Stop之间切换,但CountDown不会以我的int编号开头。如果我将起始值设置为3000,则它将完美启动。

我的问题是如何获取EditText的值作为CounterDown的起始值。

这是我对stackoverflow的第一个问题,所以请耐心等待我和我的英语不好。

最佳答案

创建一个方法

private void showCountDownTimer(int number) {
    myTimer = new CountDownTimer(number, 1000) {
            public void onTick(long millisUntilFinished)
            {
                Log.v(TAG,"CountDownTimer.onTick(): sUntilFinished: "+number+"/"+ millisUntilFinished /1000);
                textView.setText(String.valueOf(millisUntilFinished/1000));
            }
            public void onFinish()
            {
                Log.v(TAG,"CountDownTimer.onFinish()");
            }
    }.start();
}


并在onClick中调用它

Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (status == true)
            {
                Button.setText("Stop");
                number = Integer.valueOf(Time.getText().toString())*1000;
                showCountDownTimer(number);
                status = false;
                Log.d(TAG, "Button.onClick():Start " + number);
            }else
            {
                myTimer.cancel();
                Button.setText("Start");
                Log.d(TAG, "Button.onClick():Stop " + number);
                status = true;
            }

        }
    });


说明:

因为您是在onCreate上初始化myTimer(CountDownTimer)的,所以number的默认值为0。您应该即时对其进行初始化(获得该值时)。

09-06 03:19