使用全局onCLickListener [英] Using global onCLickListener

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

问题描述

是否有可能使一个onClickListener应用全局?

Is it possible to make an onClickListener app-global?

我基本都将使用相同​​的数字键盘按钮输入,而不是在每个片段的每个按钮注册和过滤单击事件我想问问是否有可能在整个应用程序共享onClickListener几个片段。

I basically have several fragments that will use the same numpad buttons for input and instead of registering and filtering click events for each button in each fragment I wanted to ask if it was possible to share an onClickListener throughout the entire app.

这是设置:

public class LoginFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle states) {
    this.context = getActivity();

    // TODO: Register onClickListener...somehow

    context.registerReceiver(receiver, filter); //For the intent listening
    view = inflater.inflate(R.layout.layout_login_screen, container, false);
    buildUI(null);
    return view;
  }

(和两个不同片段simmilar这一个)

(and two different fragments simmilar to this one)

,然后当时的想法是:

public class NumPadListener implements OnClickListener {

  @Override
  public void onClick(View v) {
    System.out.println("Yup...I'm listening?");

    // TODO: Do funny intent stuff here
  }

}

这甚至可能吗?如果是,怎么样? :)如果不是,你有没有对如何落实这一点的最佳方法什么建议?谢谢

Is this even possible? And if yes, how? :) If it isn't, do you have any recommendations on how to implement this in the best way? Thanks

推荐答案

是的,作为dymmeh显示。但是,正确的方法可能是创建一个包含所有小键盘按钮您自己的视图组件。您需要定义一个布局文件,并创建一个类将扩大某种的ViewGroup (见的复合控件的)。

Yes, as dymmeh has shown. But the proper way is probably to create your own view component containing all the numpad buttons. You would need to define a layout file, and create a class which would extend some kind of ViewGroup (see compound controls).

这个自定义组件将采取它里面的按钮的onclick事件的照顾。然后,您可以暴露一些接口(监听程序)进行的活动和片段附加到如果你需要他们的对更高级别的事件做出反应。

This custom component would take care of onClick events of buttons inside it. You could then expose some kind of interface (listener) for activities and fragments to attach to if you need them to react for higher-level events.

然后,你只包括您的自定义组件中的任何布局,你需要copypasting一堆按钮和onClick的监听器,连接code来代替。

Then, you would just include your custom component in any layouts you need instead of copypasting a bunch of buttons and the onClick listener-attaching code.

这篇关于使用全局onCLickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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