如何在android中使用CountDownTimer时从用户那里获取输入

时间:2015-09-29 19:33:01

标签: android input countdowntimer

在下面的CountDownTimer中,我想要一个以毫秒为单位的用户输入值,而不是预加载的值30000。 在这种情况下请任何人帮忙。我无法弄清楚自己。

public class MainActivity extends AppCompatActivity {

    TextView mTextField;
    EditText mEditText;
    Button mButton;


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

        mTextField = (TextView) findViewById(R.id.timerView);
        mButton = (Button) findViewById(R.id.startButton);


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Turning of the button until the timer finishes
                mButton.setEnabled(false);
                mButton.setClickable(false);

                new CountDownTimer(30000, 1000) { // Here I want to have a user input value in milli seconds instead of pre loaded value of 30000

                    public void onTick(long millisUntilFinished) {
                        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                    }

                    public void onFinish() {
                        mTextField.setText("done!");
                        // Turning on the button when the timer has finnished
                        mButton.setEnabled(true);
                        mButton.setClickable(true);
                    }
                }.start();


            }
        });

    }

}**strong text**

1 个答案:

答案 0 :(得分:2)

这很简单,为编辑文本提供用户输入的任何选项,然后将这些值作为参数传递给CountDownTimer。

// 3000 and 200 will be input from user
long millisinFuture= 3000;
long delayTime=200;

new CountDownTimer(millisinFuture, delayTime) { // Here I want to have a user input value in milli seconds instead of pre loaded value of 30000

                public void onTick(long millisUntilFinished) {
                    mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    mTextField.setText("done!");
                    // Turning on the button when the timer has finnished
                    mButton.setEnabled(true);
                    mButton.setClickable(true);
                }
            }.start();