Using the same button in other classes

前端 未结 2 1015
时光取名叫无心
时光取名叫无心 2021-01-26 02:52

I am using eclipse and are programming my first app, but I have a beginner question, i want to use some of the methods from my button in the Main class, but i want to use them i

相关标签:
2条回答
  • 2021-01-26 03:06

    If you want to use button1 in your Start Class, then you also have to declare it in the Layout file of your Start Class.

    You are getting an error most likely because that line of code will make the application search for button1 in your Start Class Layout file (which most likely isn't there).

    0 讨论(0)
  • 2021-01-26 03:10

    One option you have is to create a BaseActivity and add the Button there along with its functionality. Then you can extends BaseActivity in the Activities which you want this Button to be used.

    Doing this, you will need to use the <include> tag in the xml of your Activities that you want this Button. Now you can use this Button in other Activities and you can override the method that the Button uses if you need different functionality in certain Activities.

    If doing it that way seems too complicated then you will need to put the Button in each xml and the associated functionality in the class that uses that xml. But you might give this a try...it can really make things easier and cut down on duplicate code.

    include

    0 讨论(0)
提交回复
热议问题