How to access Activity UI from my class?

梦想与她 提交于 2019-11-26 14:15:47

问题


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 ???
    ...
}
--------------------------------------------------------------

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?


回答1:


see you post, i've edited it , to fix the problem

hope it helps :=)

here is the Edit :

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);    
            }
        });
    }
}



回答2:


  1. Your Points can't be a private class without being an inner class. So your code doesn't even compile...
  2. Pass the view as parameter to the constructor of your Points class:

    public class MyActivity extends Activity {
        TextView myView = (TextView)findViewById(R.id.myView);
        Points myPoints new Points(myView);
    
        private class Points {
            public Points(TextView view) {
                // todo
            }
        }
    }
    



回答3:


You should do everything and pass back the value to the activity to handle UI instead of doing any UI related stuff in the point stuff.




回答4:


You can pass the main Activity's context (using Points(getApplicationContext());) to the class as a constructor parameter. You could also pass the specific UI elements you want to manipulate.

A better way to do it, however, may be to have Points not know about the Activity. Have your Activity call Points methods and take the necessary actions based on the method output.




回答5:


You could just pass the view to your class.

Points myPoints = new Points(myView);

private class Points
{
  private TextView mTextView;

  Points(TextView textView)
  {
     this.mTextView = textView;
  }
}



回答6:


i was in same trouble.. i found the simple way..

  1. make a static variable and function ...
  2. call from other class..

TestActivity.java

public class TestActivity extends Activity {

   static EditText edit_text1;

   public void onCreate(Bundle savedInstanceState) 
   {   
       .....

       edit_text1 = (EditText) findViewById(R.id.edit_text1);  

       .....
   }

   public static void setMSG(String str)
   {
       edit_text1.setText(str);
   }
}

Test2.java

TestActivity.setMSG("this is text");



回答7:


Could work using an interface

file MyActivity.java:

public class MyActivity extends Activity implements Points.MyListener {

    TextView myView;
    ... onCreate(...){
        myView = (TextView)findViewById(R.id.myView);
        Points myPoints  = new Points();

        //pass in MyActivity's instance of the listener
        myPoints.addListener(this);
    }

    @Override
    public void updateTextView(String message){
        myView.setMessage(message);
    }
}

file Points.java:

public class Points {

    public Points(){

    }

    public interface MyListener{
        void updateTextView(String message);
    }
    MyListener myListener;

    public void addListener(MyListener listener){
        myListener = listener;
    }

    public void updatePoints(){
        //do some operations in calculatePoints()
        String points = calculatePoints();
        //update views using MyActivity's implementation of updateTextView()
        myListener.updateTextView(points);
    }

}

Doing it this way, events can be fired / messages sent, for lack of better terms, from the external class to update the Activity UI. This might be overkill if all sb need is to call a method in the Points class that returns something



来源:https://stackoverflow.com/questions/6030982/how-to-access-activity-ui-from-my-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!