简体   繁体   中英

How to setVisibility of a TextView in a Dialogue Box, called from outside the MainActivity Thread?

I have a textview in a dialogue box and I want to toggle the visibility from a class which is outside the MainActivity Thread.

In the MainActivity,I have the Dialog box:

addDevDialogue = new Dialog(this);
addDevDialogue.setContentView(R.layout.add_device_dialogue);

public void addDeviceDialogue ()
  {
    addDevDialogue.setTitle("Add SAndTerm Device:");
    addDevDialogue.setCancelable(false);
    addDevDialogue.show();

    incorrectIP = (TextView)addDevDialogue.findViewById(R.id.rongtxt);
    incorrectIP.setVisibility(View.INVISIBLE);
  }

And I have another class where if a particular condition meets, I want to change the visibility of the TextView which is in the dialogue box. For reference sake I ll put up the class from where I want to toggle the visibility.

The Class:

public class IP_Validation {

  Context contxt;

  public IP_Validation(Context context)
   {
     contxt = context;
   }
  public void change_state()
   {
      //toggle the TextView Visibility from here
   }
 }

Could you tell me a proper way to access the textview from a class which is outside the MainActivity?

In your activity in onCreate create new object:

    textView = (TextView) findViewById(R.id.textView);
    TestNonActivityClass test = new TestNonActivityClass(textView);
    test.doSomething();

Next, create constructor in your nonActivity class, like this:

public class TestNonActivityClass {


    private TextView textViewInNonActivityClass; //textview

    public TestNonActivityClass(TextView textView){ //constructor
        this.textViewInNonActivityClass = textView;
    }


    public void changeText(){ //some method..
        textViewInNonActivityClass.setText("Changed");
        textViewInNonActivityClass.setVisibility(View.INVISIBLE);
    }

}

If you change state of textViewInNonActivityClass, it should change on the screen..

  1. Create a handler in your Activity

     Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 1){ // set toggle visible or whatever } } } 
  2. Pass the handler to your IP_Validation

     Handler handler; public IP_Validation(Context context , Handler handler) { contxt = context; this.handler = handler; } 
  3. When you need to change the visibility , use

     handler.sendEmptyMessage(1); 

Simplest form of solution would to make this change:

public void addDeviceDialogue ()
  {

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            addDevDialogue.setTitle("Add SAndTerm Device:");
            addDevDialogue.setCancelable(false);
            addDevDialogue.show();

            incorrectIP = (TextView)addDevDialogue.findViewById(R.id.rongtxt);
            incorrectIP.setVisibility(View.INVISIBLE);
        }
        });
  }

and call the method like this:

  public void change_state()
   {
      ((MainActivity) context).addDeviceDialogue();
   }

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