gpt4 book ai didi

android - 如何从我的类(class)访问 Activity UI?

转载 作者:可可西里 更新时间:2023-11-01 18:58:03 25 4
gpt4 key购买 nike

我有一个创建我的类的对象实例的 Activity :

file MyActivity.java:
public class MyActivity extends Activity {
TextView myView = (TextView)findViewById(R.id.myView);
...
Points myPoints new Points();
...
}
--------------------------------------------------------------

file Points.java:
private class Points {
...
HOW TO USE myView HERE ???
...
}
--------------------------------------------------------------

我如何在我的类中使用 UI 对象(它不扩展 Activity )?我应该将一些上下文传递给我的 Points 类吗?具体怎么做?

最佳答案

看到你的帖子,我已经编辑了它,以解决问题

希望对你有帮助 :=)

这里是编辑:

file MyActivity.java:
public class MyActivity extends Activity {
TextView myView ;
protected void onCreate(android.os.Bundle savedInstanceState) {
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points(this);
myPoints.displayMsg("Hello World !!!");
}
}

--------------------------------------------------------------

file Points.java:
private class Points {
protected MyActivity context;
//add a constructor with the Context of your activity
public Points(MyActivity _context){
context = _context;
}

public void displayMsg( final String msg){
context.runOnUiThread(new Runnable() {

@Override
public void run() {
context.myView.setText(msg);
}
});
}
}

关于android - 如何从我的类(class)访问 Activity UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com