Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我试图设置计时器并尝试此逻辑,但它不运行?_Java_Android - Fatal编程技术网

Java 我试图设置计时器并尝试此逻辑,但它不运行?

Java 我试图设置计时器并尝试此逻辑,但它不运行?,java,android,Java,Android,运行此代码时,它直接将文本设置为0,而不是以1秒的间隔将文本从30减少到0 JAVA代码 public void LoadCounter() { for (int i = 30; i > -1; i--) { final int counter = i; new Handler().postDelayed(new Runnable() { @Override public void run() {

运行此代码时,它直接将文本设置为0,而不是以1秒的间隔将文本从30减少到0

JAVA代码

public void LoadCounter() {

    for (int i = 30; i > -1; i--) {
        final int counter = i;

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                txtview.setText("" + counter);
                Log.d("test1", "" + counter);
            }
        }, 1000);


    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/txtview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="#000"
    android:layout_centerInParent="true"
    android:textStyle="bold"/>
<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtview"
    android:layout_centerInParent="true"
    android:text="START COUNT"
    android:textStyle="bold"
    android:textSize="20sp"/>
运行时设置值的文本视图

XML代码

public void LoadCounter() {

    for (int i = 30; i > -1; i--) {
        final int counter = i;

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                txtview.setText("" + counter);
                Log.d("test1", "" + counter);
            }
        }, 1000);


    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/txtview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="#000"
    android:layout_centerInParent="true"
    android:textStyle="bold"/>
<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtview"
    android:layout_centerInParent="true"
    android:text="START COUNT"
    android:textStyle="bold"
    android:textSize="20sp"/>

我知道倒计时法是一种选择,但我正试图用这种方法来解决这个问题 如对上述代码或解释有任何更改,我们将不胜感激

这是我第一次问有关StackOverflow的问题,我希望这是一个正确的提问方式,并提前感谢您的帮助

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        txtview.setText((millisUntilFinished / 1000) + "");
    }

    public void onFinish() {
         // todo
    }

}.start();

我们又发明了轮子:)


我自己想出来的,它工作得很好

public void LoadCounter() {

    for (int i = 0; i <= 30; i++) {
        final int counter = (30 - i);
        int TIME = 1000 * i;


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                txtview.setText(String.valueOf(counter));
            }
        }, TIME);


    }
}
public void LoadCounter(){

对于(int i=0;我想你可能会喜欢,我可以使用倒计时方法,但我想知道我的逻辑出了什么问题。为什么它不能正常工作?这段代码在没有任何停顿的情况下立即创建了大量的
处理程序和
可运行的
,所以它工作错误。对不起,它看起来像是发明了一个轮子。@DimaKozhevin Thanks很多。我使用这种方法只是为了学习。不客气。谢谢你提出的有趣的问题。我知道我可以使用倒计时法,但我想知道我的逻辑有什么问题。为什么它不能正常工作?