[英]Java: How to do TextView.setText in another package?
I'm new to Java, I'm trying to build something in Android Studio. 我是Java新手,正在尝试在Android Studio中构建一些东西。 The point is to 'push' a value for TextView baseTickVar in class BaseScreen, from another PACKAGE, where the class CoreFunctionality resides.
关键是要从另一个类CoreFunctionality所在的PACKAGE中“推送” BaseScreen类中TextView baseTickVar的值。 Each time the tickNumber increases I want that shown in the TextView, so it's not a one time setting of text.
每次tickNumber增加时,我都希望在TextView中显示它,因此这不是一次性设置文本。
I have tried interfaces, but interfacing won't allow variables, only constants. 我已经尝试过接口,但是接口将不允许变量,仅允许常量。 I've tried TextView.setText from the CoreFunctionality package, but that gave a nullpointerException and declaring the TextView to counter that didn't seem to help.
我已经尝试过CoreFunctionality包中的TextView.setText,但是它给出了nullpointerException并声明了TextView来对抗似乎没有帮助。
public class BaseScreen extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_screen);
// some irrelevant code here so i left it out.
TextView baseTickVar = (TextView)findViewById(R.id.baseTickVar);
baseTickVar.setText("1"); // just to not have it empty...
}
Now I want to set value of baseTickVar with a variable from the other package CoreFunctionality 现在,我想使用另一个包CoreFunctionality中的变量设置baseTickVar的值
public class CoreFunctionality extends Activity implements Runnable {
Thread tickThread = null;
volatile boolean playingGalactic;
long lastTick;
public int tickNumber;
int tickLength;
TextView baseTickVar;
public void controlTicks() {
tickLength = 2000;
long timeThisTick = (System.currentTimeMillis() - lastTick);
long timeToWait = tickLength - timeThisTick;
if (timeToWait > 0) {
try {
tickThread.sleep(timeToWait);
} catch (InterruptedException e) {
}
}
lastTick = System.currentTimeMillis();
}
@Override
public void run() {
while (playingGalactic) {
controlTicks();
tickNumber++;
Log.i("Tick number ", "" + tickNumber);
updateTick();
}
}
private void updateTick() {
// this is the whole point...
baseTickVar.setText("" + tickNumber);
}
public void resume() {
playingGalactic = true;
tickThread = new Thread(this);
tickThread.start();
} }
I guess your BaseScreen
is the main screen and CoreFunctionality
is some component that is doing some work. 我猜您的
BaseScreen
是主屏幕,而CoreFunctionality
是正在执行某些工作的某些组件。 Actually CoreFunctionality
does not need to be Activity
, it better fits to be a service. 实际上,
CoreFunctionality
不必是Activity
,它更适合作为服务。
baseTickVar
to the CoreFunctionality
. baseTickVar
引用baseTickVar
给CoreFunctionality
。 TextView
) from within another thread. TextView
)。 You should consider using some inter-thread communication (such as Message
). Message
)。 Make BaseScreen
to extend Handler
or make a Handler
object in it then override 使
BaseScreen
扩展Handler
或在其中创建Handler
对象,然后覆盖
public void handleMessage(Message msg) {
baseTickVar.setText("" + msg.obj);
}
In CoreFunctionality
在
CoreFunctionality
private void updateTick() {
Message msg=new Message();
msg.obj=tickNumber;
h.sendMessage(msg);
}
Of course you'll have to pass the h
reference to CoreFunctionality
. 当然,您必须将
h
引用传递给CoreFunctionality
。
Maybe not 100% accurate but it should work with little tweaking. 也许不是100%准确,但应该稍作调整即可。 Hope this will help.
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.