上多重活动相同的按钮 [英] Same button on multiple activities

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

问题描述

右键我在按钮上的每一次活动在同一个地方刚好位于一个标志,我有回合20活动的时刻,但会提高很快上升了很多,我真的不希望复制和粘贴相同code在每项活动,所以我在寻找一种简单,有效的解决方案来处理onClick事件,这将在全球各地工作的应用程序。

right i have a sign in button located exactly in the same place on every activity and i have bout 20 activities at the moment but will rise a lot higher soon, and i don't really want to be copying and pasting the same code in to each activity, so I'm looking for a simple, efficient solution to handle the onClick event which will work globally throughout the app.

例如,如果用户A点击按钮,在登录活动1和的迹象,这将表明他签约在活动2和3等..直到注销。

For example, if User A clicks on the sign in button on Activity 1 and signs in, it will show that he is signed in on Activity 2 and 3 and so on.. until they log out.

在按钮的符号则在整个应用程序,它是@ + ID /签到同一个ID

The sign in button has the same ID throughout the whole application which is "@+id/signIn"

难道是更容易调用一个函数在每个活动的开始?我认为这会不会是每个有效地利用处理能力等?!

Would it be easier to call a single function at the beginning of each activity? I thought that wouldn't be every effective use of processing power etc?!

任何建议和/或指导将是非常美联社preciated。谢谢:)

Any suggestions and/or guidance would be much appreciated. Thank you :)

推荐答案

您无法避免实施该监听器在所有的活动中任一方式。但是你可以做一个更多的有组织的方式:

You can't avoid implementing that listener in all of your activities in either ways. But you can do it in a bit more organized way:

中,你必须登录按钮,点击你可以写一个自定义标题布局应用程序(<$ C C $> /res/layout/header.xml ),监听器设置(指向一个 onSignInClicked 法):

You could write a custom header layout for your application (/res/layout/header.xml), in which you have the "Sign In" button with a click listener set (pointing to an onSignInClicked method):

android:onClick="onSignInClicked"

然后你有这个头给每个活动的布局:

Then you include this header to each activity layout:

<include android:id="@+id/header" layout="@layout/header" />

您也可以创建一个包含一个 onSignInClicked 方法声明的接口,并通过所有的活动实现该接口可以强制其定义 onSignInClicked 法的身体。

You could also create an interface which contains an onSignInClicked method declaration, and by all your activities implementing that interface you force them to define the onSignInClicked method's body.

什么你其实这里也可以包装成

What you actually do there can also be wrapped into

  • 在里面全局静态方法 访问类,或
  • 在里面一个良好的参数化方法 你的应用程序扩展类。
  • a static method inside a globally accessible class, or
  • a well-parametrized method inside your Application extension class.

因此​​,在所有的活动中这种方法可以是:

so in all of your activities this method can be:

public static void onSignInClicked(View view)
{
    // static method with call with reference to the current activity
    SignInHelper.doSignIn(this);
}

public static void onSignInClicked(View view)
{
    // global method in your `Application` extension
    // with reference to the current activity
    ((MyApplication)getApplicationContext()).doSignIn(this);
}

如果你选择了第二种方式,不要忘了更新你的 androidManifes.xml 通过设置名称你的应用程序属性标签:

If you choose the second way, don't forget to update your androidManifes.xml by setting the name attribute of your application tag:

<application android:name=".MyApplication" [...]

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

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