简体   繁体   English

从另一个类访问Activity中的UI元素

[英]Accessing UI elements in Activity from another class

I have a problem with that code 我的代码有问题

setContentView(R.layout.game);
TextView text = (TextView) findViewById(R.id.qwe_string);
text.setText("Hello Android");

It works if it's inside an activity, but if it isn't, obviously, it gives errors: 如果它在活动中,它就可以工作,但如果不在活动中,则很明显会出现错误:

The method findViewById(int) is undefined for the type new TimerTask(){}
The method setContentView(int) is undefined for the type new TimerTask(){}

The code is inside a timer in separate class (not activity). 该代码位于单独类中的计时器内(不是活动)。 The full code is as follows. 完整的代码如下。

//main timer task for ingame time processing
static Timer gameTimer = new Timer();
static TimerTask gameTimerTask = new TimerTask() {
    @Override
    public void run() {
        setContentView(R.layout.game);
        TextView text = (TextView) findViewById(R.id.qwe_string);
        text.setText("Hello Android");
    }
};

I tried changing it like that 我试图像这样改变它

ScreenGame.this.setContentView(R.layout.game);
TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
text.setText("Hello Android");

But it still doesn't work :( 但这仍然行不通:(

PS - Yes, I searched other similar questions, but all of them although appear to be the same, in fact completely different. PS-是的,我搜索了其他类似的问题,但是所有这些问题看起来都一样,但实际上完全不同。

ScreenGame.this.setContentView(R.layout.game);
TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
text.setText("Hello Android");

is exactly the same as 与...完全相同

setContentView(R.layout.game);
TextView text = (TextView) findViewById(R.id.qwe_string);
text.setText("Hello Android");

wrong code 错误代码

You should make a parent class, that fires off all the other Activities. 您应该创建一个父类,该类会触发所有其他活动。 In that parent class, you can have a reference to each child activity, and you can ask each child for it's textfield: 在该父类中,您可以引用每个孩子的活动,也可以向每个孩子询问其文本字段:

// In the parent:
child.getTextView().setText("Hello galaxy!");

// with the child method:
TextView getTextView () {
  return (TextView) findViewById(R.id.qwe_string);
}

EDIT: More info: 编辑:更多信息:

The code I gave was not good, I hadn't understood your problem well... I hope I do now, do I'll try to correct myself. 我提供的代码不好,我不太了解您的问题...希望我现在就这样做,我会尽力纠正自己的问题。

Make a seperate class, for example MyTimer , which will extends the TimerTask class: 创建一个单独的类,例如MyTimer ,它将扩展TimerTask类:

class MyTimer extends TimerTask {
  // your class
}

Make a constructor, that excepts an TextView as an argument, and saves a reference to it. 构造一个构造函数(将TextView作为参数除外)并保存对其的引用。

TextView theTextView;
MyTimer (TextView tv) {
  this.theTextView = tv;
}

Now implement run() : 现在实现run()

@Override
public void run() {
    setContentView(R.layout.game);
    thetextView.setText("Hello Galaxy!");
}

Call make this class using the following code: 使用以下代码调用make此类:

static TimerTask gameTimerTask = new MyTimer((TextView) findViewById(R.id.qwe_string));

I hope this is all correct, I have to do this from memory as I don't have any testing environment. 我希望这都是正确的,因为我没有任何测试环境,因此必须从内存中执行此操作。 At least it should help you in the right direction. 至少它应该在正确的方向上为您提供帮助。

You will need: 你会需要:

ScreenGame.this.runOnUiThread( new Runnable(){
  public void run(){
   TextView text = (TextView) ScreenGame.this.findViewById(R.id.qwe_string);
   text.setText("Hello Android");  }
 });

And you should call the following line in onCreate() of ScreenGame : 您应该在ScreenGame onCreate()中调用以下行:

setContentView(R.layout.game);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM