如何从我的班级访问活动 UI? [英] How to access Activity UI from my class?
本文介绍了如何从我的班级访问活动 UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个活动,它创建了我的类的对象实例:
I have an activity which creates an object instance of my class:
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 对象(它不扩展活动)?我应该将一些上下文传递给我的 Points 类吗?我该怎么做?
How do I use the UI objects in my class (which does not extend an Activity)? Should I pass some context to my Points class? How do I do, exactly?
推荐答案
看到你的帖子,我已经编辑了它,以解决问题
see you post, i've edited it , to fix the problem
希望有帮助:=)
这里是
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);
}
});
}
}
这篇关于如何从我的班级访问活动 UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文