Android-简单线程循环不';行不通

Android-简单线程循环不';行不通,android,multithreading,Android,Multithreading,我想要一个每次改变字符串的线程循环。(例如d->dd->ddd->dddd…)我希望它在执行线程的同时更新视图,但屏幕在第一次运行()执行时停止(即“dd”)。怎么了?谢谢 package com.example.name.app; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; i

我想要一个每次改变字符串的线程循环。(例如d->dd->ddd->dddd…)我希望它在执行线程的同时更新视图,但屏幕在第一次运行()执行时停止(即“dd”)。怎么了?谢谢

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;
                }
            }
        }
    }

}

所有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;
                }
            }
        }
}

所有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;
                }
            }
        }
}

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

如果需要,创建一个简单的处理程序并更新主线程

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);

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

如果需要,创建一个简单的处理程序并更新主线程

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);

试试这个..它将提供与您想要的相同的输出。

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;
                }
            }
        }
    }

试试这个..它将提供与您想要的相同的输出。

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;
                }
            }
        }
    }

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;
                }
            }
        }
    }
}
活动\u 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>


希望这对您有所帮助。

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;
                }
            }
        }
    }
}
活动\u 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>


希望这对您有所帮助。

必须在UI线程中更新UI元素。可以使用处理程序发布,也可以使用
runOnUiThread
更新
TextView
。根据命名约定,类名也使用CamelCase。UI元素必须在UI线程中更新。可以使用处理程序发布,也可以使用
runOnUiThread
更新
TextView
。根据命名约定,也使用CamelCase作为类名。欢迎@ysd,并感谢您将其标记为正确答案。很高兴帮助你。继续编码。欢迎@ysd,并感谢您标记为正确答案。很高兴帮助你。继续编码。