How to access Activity UI from my class?

前端 未结 7 1112
情歌与酒
情歌与酒 2020-12-01 14:53

I have an activity which creates an object instance of my class:

file MyActivity.java:
public class MyActivity extends Activity {
    TextView myView = (Text         


        
相关标签:
7条回答
  • 2020-12-01 15:08

    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");
    
    0 讨论(0)
  • 2020-12-01 15:11

    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);    
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-01 15:12

    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.

    0 讨论(0)
  • 2020-12-01 15:13

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-01 15:15
    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
              }
          }
      }
      
    0 讨论(0)
  • 2020-12-01 15:17

    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.

    0 讨论(0)
提交回复
热议问题