Android - 简单的线程循环不起作用

时间:2017-12-11 11:10:46

标签: android multithreading

我想要一个每次更改String的线程循环。 (例如d - > dd - > ddd - > dddd ...)我希望它更新视图以及线程被执行,但屏幕在第一次运行()执行时停止(这是&# 34; DD&#34)。怎么了?谢谢!

package com.example.name.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread t1 = new thread();
                t1.start();
            }
        });
    }

    private class thread extends Thread {

        TextView textView = (TextView) findViewById(R.id.tv);

        @Override
        public synchronized void run() {

            String text = "d";
            while (true) {
                try {
                    text += "d";
                    textView.setText(text);
                    sleep(5000);
                } catch (Exception e) {
                    return;
                }
            }
        }
    }

}

4 个答案:

答案 0 :(得分:1)

所有ui元素都在主线程上处理。因此,要更新任何ui元素,您必须从主线程(UI线程)执行此操作。您可以使用runOnUiThread切换到UI线程:

TextView textView;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread t1 = new thread();
                t1.start();
            }
        });
    }

 private class thread extends Thread {

        @Override
        public synchronized void run() {

            String text = "d";
            while (true) {
                try {
                    text += "d";
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(text);
                        }
                    });

                    sleep(5000);
                } catch (Exception e) {
                    return;
                }
            }
        }
}

答案 1 :(得分:1)

试试这个..它会提供你想要的相同输出。

public class TestActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        textView = (TextView) findViewById(R.id.tv);
        final Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread t1 = new thread();
                t1.start();
            }
        });
    }

    private class thread extends Thread {

        String text = "d";

        @Override
        public synchronized void run() {

            while (true) {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(text);
                        }
                    });
                    sleep(5000);
                    text += "d";
                } catch (Exception e) {
                    return;
                }
            }
        }
    }

答案 2 :(得分:1)

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        final Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread t1 = new thread();
                t1.start();
            }
        });
    }

    private class thread extends Thread {

        String text = "d";

        @Override
        public synchronized void run() {

            while (true) {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(text);
                        }
                    });
                    sleep(5000);
                    text += "d";
                } catch (Exception e) {
                    return;
                }
            }
        }
    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="digitdemo.com.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Count"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Press"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

<强>输出:

enter image description here

希望这会对你有所帮助。

答案 3 :(得分:0)

而不是为简单的递增和更新UI任务扩展线程类使用handler.Handler在具有不同执行环境的同一主线程中运行。

创建一个简单的处理程序并根据需要更新主线程。

TextView textView = (TextView) findViewById(R.id.tv);
String text = "d";

Runnable runnable=null;
Handler newHandler=null;

private void runTask() {
    newHandler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            try {
                //update UI
                text += "d";
                textView.setText(text);
                newHandler.post(runnable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    newHandler.post(runnable);
   }
}

并停止使用处理程序

newHandler.removeCallbacks(runnable);