如何从我的班级访问活动的用户界面? [英] How to access Activity UI from my class?
本文介绍了如何从我的班级访问活动的用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的创建类的对象实例的活动:
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对象(不延伸的 活动)?我应该通过一些背景,以我的观点类?我该怎么办,到底是什么?
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);
}
});
}
}
这篇关于如何从我的班级访问活动的用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文