search

Home  >  Q&A  >  body text

java - 在另一个非activity类中如何调用并操作当前activity界面上的元素?

初学,在尝试自己写。先谢过各位!

大家都回家得很好,已经有思路了也学到了很多,再次感谢。

大家讲道理大家讲道理2899 days ago476

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-18 09:57:57

    If the class activity you mentioned holds an instance, then you can directly pass the instance of the view to it for operation. However, you will usually write a method in the activity to operate the view, and then let other classes call this method, because the instance of the view The creation and destruction are related to the life cycle of the activity, so it is best not to let classes outside the activity directly hold and operate the views in this activity. In addition, be careful to operate the views on the UI thread

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:57:57

    Non-activity? Is it a fragment? service? Or what? No matter where you update, it's a communication issue between them.
    Recommendations:
    1. You can use interfaces for communication
    2. You can use the EventBar event bus open source library, so that you can learn the use of an open source library

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:57:57

    You are talking about operating View in a non-Activity class. Currently, most of them adopt the MVP development model and use the interface callback mechanism to operate View. However, MVP has pros and cons. When operating View, MVP must ensure the persistence of View. deal with.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:57:57

    First of all, you need to know the relationship between your class and this activity. If it is a helper class, then this activity should have an instance of this class. Then you only need to call a corresponding method of this instance. If this class needs To operate a view object or other object in the activity, pass the object as a parameter into the method.
    If it is other classes, such as service or broadcast, just learn how to use these components
    I hope it will be helpful to you,,

    reply
    0
  • Cancelreply