使用editText倒计时

时间:2017-05-28 19:39:36

标签: java android countdowntimer

我想创建一个倒计时,其中初始数字由用户给出。我创建了这个应用程序但是当应用程序开始关闭时。我不明白错误是什么,因为在编译过程中没有错误。我究竟做错了什么? 这是代码:

public class Main2Activity extends AppCompatActivity {

Button bstart, bStopReset;
EditText editTimer;
CountDownTimer timer;


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

    editTimer = (EditText) findViewById(R.id.contatore);
    bstart = (Button) findViewById(R.id.start);
    bStopReset = (Button) findViewById(R.id.stop_reset);
}

String valore = editTimer.getText().toString();

int ValoreIntero = Integer.parseInt(valore);


public void startOnClick(View view) {

    timer = new CountDownTimer(ValoreIntero, 1000) {
        @Override
        public void onTick(final long millSecondsLeftToFinish) {
            String time = String.valueOf(millSecondsLeftToFinish / 1000);
            editTimer.setText(time);
        }

        @Override
        public void onFinish() {
            editTimer.setText("Done!");
        }
    };
    timer.start();
}

public void stopOnClick(View view) {
    timer.cancel();
    editTimer.setText("0");
    }

}

的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context="com.example.gabrypacor.orologio.Main2Activity">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.39"
    android:gravity="center"
    android:text="countdown"
    android:textAlignment="center"
    android:textSize="36sp" />

<TextView
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.27"
    android:gravity="center"
    android:text="inserisci il tempo del countdown nel riquadro blu"
    android:textAlignment="center"
    android:textSize="20sp"
    tools:textAlignment="center" />

<EditText
    android:id="@+id/contatore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.21"
    android:background="@android:color/holo_blue_light"
    android:ems="10"
    android:inputType="numberDecimal"
    android:text="0"
    android:textAlignment="center"
    android:textColorLink="?android:attr/textColorPrimaryDisableOnly"
    android:textSize="60sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.18"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:onClick="startOnClick"
        android:text="inizio" />

    <Button
        android:id="@+id/stop_reset"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:onClick="stopOnClick"
        android:text="stop/reset" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您在初始化活动时设置这两个值,而不是在用户单击开始按钮时设置这两个值:

{{1}}

将它们移动到startOnClick方法中,这应该可以解决您获得的异常。