简体   繁体   中英

Android: Changing a textView from a standard java class outside an Activity

So I am curious if it is possible to change a TextView from another java class in Android.I know how to change it in the activity and have no issue doing that. But for reasons I am looking to pull out the UI changes into another file.

layout file

   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Test UI"
       android:textAlignment="center"
       android:onClick="testUI"/>
   <TextView
       android:id="@+id/someText"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="SomeText"
       android:textAlignment="center"/>

Here would be the code that I would have in the main activity

    SetSomeText setSomeText = new SetSomeText();

    public void testUI(View v){
        setSomeText.Something();
    }

Then I would have a standard java class that would allow me to change the TextView

public class SetSomeText extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_someNewActivity);
    }

    public void Something(){
        TextView someNewText = findViewById(R.id.someText);
        someNewText.setText("Something");
    }
}

This ends up giving me an illegal state exception. I know this is not a normal way to do this. Just curious if something like this can be done.

    java.lang.IllegalStateException: Could not execute method for android:onClick

Maybe you could do something like this in the class

public void Something(TextView myView){
    myView.setText("Something");
}

And you can use it in main

MyClass class = new MyClass();

TextView someNewText = findViewById(R.id.someText);
class.Something(someNewText);

Use interface to communicate between activity and java class.

Steps to achieve this using interface .

  1. Implement the interface in your activity.
  2. Pass the instance of the interface to the java class.
  3. With the help of the instance you can call the method which is implemented in your activity.

If you want to communicate between 2 different activities. Use startActivityForResult and setResult methods.

  • You shouldn't create an object of type Activity by your self you should start a new activity by using Intents

  • Only the Activity that created the view can Access it using findViewById(int); ... so calling it from another activity (in SomeThing() function) would return null

  • If you wanted to make changes in a view using another class then you can do something like this:

     public class myAwesomeClass { TextView tv; public myAwesomeClass(TextView v){ this.tv = v; } public void doSomething(){ tv.setText("This would work with no errors"); } } //and In the Activity class myAwesomeClass mAC; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); mAC = new myAwesomeClass(findViewById(R.id.someText)); } public void testUI(View v){ mAC.doSomething(); }

sorry for the bad writing.. Im using my phone:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM