使用其他类相同的按钮 [英] Using the same button in other classes

查看:97
本文介绍了使用其他类相同的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse和正在编写我的第一个应用程序,但我有一个初学者的问题,我想在主类中使用的一些方法从我的按钮,但我想在我的启动类使用它们,怎么办我设置呢?

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 in my Start class, how do i set this up?

它是通过使用:

Button1 = (Button) find View By Id(R.id.button1)

在每堂课我想使用它?还是有更简单的方法?

In every class i want to use it? Or is there a much simpler way?

我不知道这会给我正确的结果,我目前得到一个错误,我不知道这是否是故障。

I am not sure that this will give me the correct result, i am currently getting an error, and i don't know if this is the fault.

感谢您的帮助。

推荐答案

你有一个选择是创建一个 BaseActivity 并添加按钮存在以及它的功能。然后你可以延伸BaseActivity 活动要在此按钮来使用。

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.

这样做,你就需要使用<包括> 的XML标签活动的你想要这个按钮。现在,你可以在其他使用按钮活动您可以覆盖如果你需要在某些按钮使用方法>活动。

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.

如果做这样的说法似乎过于复杂,那么你将需要把每个XML的按钮和相关的功能,在使用了XML的类。但你可能会试试这个...它真的可以让事情变得更容易,并减少重复code。

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.

包括

这篇关于使用其他类相同的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆