如何将变量从线程传递到外部环境? [英] How to pass variables from the thread to the outside environment?

查看:284
本文介绍了如何将变量从线程传递到外部环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主活动中嵌套了一个线程:

I have a Thread nested in the main activity:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    new Thread(new Runnable(){
        public void run() {
            int myInt = 1;
            // Code below works fine and shows me myInt
            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText(myInt);
        }
    }).start();

    // Code below doesn't work at all
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(myInt);

}

我不确定这个结构是否正确.我应该如何将 myInt 变量传递给MainActivity,以使其在线程外变得可识别和可操作?

I'm not sure if this structure is correct at all. How should I pass myInt variable to the MainActivity so it will become recognizable and operable outside the Thread?

推荐答案

在尝试设置线程外(在主线程上)的textview之前,您首先需要一个已设置整数的全局变量.需要预先设置它,因为您启动的新线程将仅启动并移至下一行代码,因此尚未设置myInt.

You would first want a global variable for the integer that is already set before you try setting the textview that's outside of your thread (on the main thread). It needs to be set beforehand because the new thread you start will simply be started and move along to the next lines of code, so myInt won't have been set yet.

然后,至少在最初,对主线程上的textview使用预定的全局整数值.如果要在启动的线程中进行更改,请在类中创建一个类似setIntValue()的方法,该方法将从线程中传入整数并将全局变量设置为该值.如果您愿意,我可以稍后更新代码示例.

Then, use the predetermined global integer value for the textview on the main thread, at least initially. If you would like to change it from within the thread you started, then create a method in your class like setIntValue() which will pass in the integer from the thread and set the global variable to that value. I can update with code examples later if you'd like.

更新:示例代码

public class MainActivity extends Activity {

//your global int
int myInt

public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

new Thread(new Runnable(){
    public void run() {
        int myRunnableInt = 1;
        // Code below works fine and shows me myInt
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(String.valueOf(myRunnableInt));

        //say you modified myRunnableInt and want the global int to reflect that...
        setMyInt(myRunnableInt);
    }
}).start();

//go ahead and initialize the global one here because you can't directly access your
myRunnableInt
myInt = 1;

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(String.valueOf(myInt)); //now you will have a value here to use

//method to set the global int value
private void setMyInt(int value){
    myInt = value;

    //you could also reset the textview here with the new value if you'd like
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(String.valueOf(myInt));
}
}

注意:如果只希望能够重置textview,而不是拥有一个可操作的全局变量,我建议将方法更改为仅传入新的整数并设置textview,而不是存储一个global变量,如下所示:

Note: If you only wish to be able to reset the textview, as opposed to having a global, operable variable, I would suggest changing the method to just pass in the new integer and set the textview, rather than storing a global variable, like this:

private void setTextView(int newInt){
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(String.valueOf(newInt));
}

如果执行上述操作,请确保从线程内调用该方法时,请在UI线程上进行调用,如下所示: runOnUiThread(new Runnable()){ public void run(){ //更新UI元素 } }

If you do the above, make sure when you call the method from within the thread, call it on the UI thread like so: runOnUiThread(new Runnable()){ public void run(){ //update UI elements } }

这篇关于如何将变量从线程传递到外部环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆