How to access Activity UI from my class?

前端 未结 7 1165
情歌与酒
情歌与酒 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");
    

提交回复
热议问题