用户输入实现倒数计时器

时间:2014-03-09 11:01:13

标签: android

我正在尝试实现倒数计时器,该计时器从用户在EditText中输入的数字开始。当我使用模拟器运行我的程序时,我意识到倒计时器块内部出现了问题,因为它没有进入该块内。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText countDownTxt = (EditText) findViewById(R.id.editText2);
    final EditText intervalTxt = (EditText) findViewById(R.id.editText1);

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
                    int interval = Integer.parseInt(intervalTxt.getText().toString());
                    Log.d("buttonpressed", "interval  countdown equals " + interval);

                    new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {

                        public void onTick(final long millisUntilFinished) {
                            Log.d("counttimer1", "enter");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                       countDownTxt.setText("" + millisUntilFinished / 1000);
                                }
                            });
                               countDownTxt.setText(""+ millisUntilFinished / 1000);
                            }



                        public void onFinish() {
                            countDownTxt.setText("done!");
                        }
                     }.start();

                    }
            }
    );

有关解决此问题的任何建议吗?

2 个答案:

答案 0 :(得分:3)

尝试使用此

    long timeVal = Long.parseLong(intervalTxt.getText().toString());
    new CountDownTimer(timeVal, 1000) {
    }

答案 1 :(得分:0)

如果您希望按用户输入的数字为秒,您应该具有:

int time = Integer.parseInt(intervalTxt.getText().toString()) * 1000;

new CountDownTimer(time, 1000) {

...