使用Edittext进行倒数计时器

时间:2018-04-27 23:58:33

标签: java android-studio countdowntimer

我找到倒数计时器的代码,但值为10秒。我希望它从Edittext获取值,这是代码

private static final long TIMER_LENGHT = 10; // Ten seconds
    private long mTimeToGo;
    private CountDownTimer mCountDownTimer;
    private TimerState mState;

我做到了

Edittext1 et1 = (EditText)findViewById(R.id.Edittext1);
String strInput = et1.getText().toString();
private static final long TIMER_LENGHT = et1; 
private long mTimeToGo;
private CountDownTimer mCountDownTimer;
private TimerState mState;

但是我无法做些什么才能让它发挥作用* this is the Full code i'm using

1 个答案:

答案 0 :(得分:0)

不应该这样:

private static final long TIMER_LENGHT = et1;

是:

private static final long TIMER_LENGHT = Long.parseLong(strInput);

编辑:

首先将TIMER_LENGTH更改为:

  private long TIMER_LENGHT = 0;

我们将以0开始,稍后当我们点击我们更改它的按钮时:

@OnClick(R.id.main_timer_button)
    public void onButtonClicked() {
        if  (mState == TimerState.STOPPED) {
            mPreferences.setStartedTime(getNow());
            EditText et1 = (EditText)findViewById(R.id.et1);
            String strInput = et1.getText().toString().trim();
            TIMER_LENGHT = Long.parseLong(strInput);
            startTimer();
            mState = TimerState.RUNNING;
        }
    }