我怎么能传递一个变量到一个新的可运行的声明? [英] how can I pass a variable into a new Runnable declaration?

查看:204
本文介绍了我怎么能传递一个变量到一个新的可运行的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

 的Runnable做=新的Runnable()
    {
        公共无效的run()
        {
            的System.out.println(你好);
        }
    };
 

然后在我的Andr​​oid的活动,我会打电话是这样的:

  runOnUIThread(完成);
 

然后我打电话。不过,我想这你好不硬codeD,这样我就可以通过它。否则我就必须有这些声明为每一位字符串我想打印。

(这实际上是一个Android的问题,但精简下来到基本的Java所以它比较容易回答)

感谢

解决方案

 最后字符串Hello = whereverItsComingFrom;
可运行做=新的Runnable()
    {
        公共无效的run()
        {
            的System.out.println(你好);
        }
    };
 

I have the following :

Runnable done = new Runnable()
    {
        public void run()
        {
            System.out.println("Hello");
        }
    };

And then in my Android activity I'll call something like :

runOnUIThread(done);

Which I then call. However, I want that "Hello" not to be hardcoded, so I can pass it in. Otherwise I'll have to have one of these declarations for every String I want to print.

(This is actually an android question, but slimmed it down to basic Java so its easier to answer)

Thanks

解决方案

final String hello = whereverItsComingFrom;
Runnable done = new Runnable()
    {
        public void run()
        {
            System.out.println(hello);
        }
    };

这篇关于我怎么能传递一个变量到一个新的可运行的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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