简体   繁体   中英

How do i call activity method from custom view?

I have a main activity that calls setContentView() on a custom view that I created using something like public myCustomView extends View{} . My main question is that I have a method inside of the main activity, functionIWantToCall() and I want to call it from the onDraw() method of the customview because that's where all the action happens. How can I do this? Alternatively, having a method that repeats itself in the main activity could work too, similar to calling invalidate() in the onDraw method. Can anyone tell me how to go about either of these methods? I appreciate the help! =)

You could create a static method inside your MainActivity class, which you could then call by doing MainActivity.functionIWantTocall() . This will only work so long as you don't have any other function that are object-based inside the method. If you do, you'll get an error that says you can't call non-static methods from a static method.

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