简体   繁体   English

如何将 onClick 函数传递给 Intent

[英]How to pass onClick function to Intent

I want to modularize the usage of my class but I have problem in passing function.我想模块化我的类的使用,但我在传递函数时遇到问题。 I want to be able to pass an OnClickListener from 1 activity to this CoachmarkActivity .我希望能够将OnClickListener从 1 个活动传递给这个CoachmarkActivity

I tried 2 different method: 1. Passing an OnClickListener to Intent 2. Passing a class, FollowUpClass, implements Serializable, which has method onClick.我尝试了两种不同的方法: 1. 将 OnClickListener 传递给 Intent 2. 传递一个类 FollowUpClass,实现了 Serializable,它具有 onClick 方法。

You can see the code below.你可以看到下面的代码。 It is not complete code, but you should be able to comprehend this.这不是完整的代码,但您应该能够理解这一点。

public class CoachmarkActivity extends Activity {

    public static final String RES_LAYOUT = "RES-LAYOUT";
    public static final String LISTENER = "LISTENER";
    public static final String FOLLOW_UP = "FOLLOW-UP";

    @Override protected void onCreate(Bundle savedInstance) {

        setContentView(getIntent.getIntExtra(RES_LAYOUT, R.layout.activity_default))

        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);

        // 1ST ATTEMPT        
        // I want to modularize this
        OnClickListener onClickPassedFromIntent = (OnClickListener) getIntent().getSerializableExtra(LISTENER);
        button1.setOnClickListener(onClickPassedFromIntent);

        // 2ND ATTEMPT
        final FollowUpListener folllowup = (FollowUpListener) getIntent().getSerializableExtra(FOLLOW_UP);
        button2.setOnClickListener(new OnClickListener() {
            @Override void onClick() {
                // !! Here is error, exception thrown
                folllowup.onClick();
            }
        });
    }

    /**
     * Public method to be used in other activity.
     * Invocation wanna be:
     *   CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new OnClickListener() {
     *      @Override void onClick() {
     *          // Do something
     *      }
     *   });
     */
    public static void startThisActivity(Context context, int resId, OnClickListener listener) {
        Intent intent = new Intent(context, CoachmarkActivity.class);
        intent.putExtra(RES_LAYOUT, resId);
        // !! Line below is error, onClickListener is not serializable, no method can accomadate below
        intent.putExtra(LISTENER, listener);
        context.startActivity(intent);
    }

    /**
     * Public method to be used in other activity.
     * Invocation wanna be:
     *   CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new FollowUpListener() {
     *      @Override void onClick() {
     *          // Do something
     *      }
     *   });
     */
    public static void startThisActivity(Context context, int resId, FollowUpListener folllowup) {
        Intent intent = new Intent(context, CoachmarkActivity.class);
        intent.putExtra(RES_LAYOUT, resId);
        intent.putExtra(FOLLOW_UP, followup);
        context.startActivity(intent);
    }
}

The abstract class:抽象类:

public abstract class FollowUpListener implements Serializable {
    public abstract void onClick();
}

The problems are stated in the comment in source code above, with tag "!!"问题在上面源代码的注释中说明,标签为“!!” (Just CTRL+F "!!"). (只需 CTRL+F“!!”)。 What I want to do is like passing a Delegate object (function in form of variable) in C#, but in Android Java.我想要做的就像在 C# 中传递一个 Delegate 对象(变量形式的函数),但在 Android Java 中。

Any idea?有什么想法吗? Thanks.谢谢。

You are trying to add a Serializable extra to your Intent, but OnClickListener does not implement that interface.您正在尝试向 Intent 添加Serializable额外内容,但OnClickListener未实现该接口。 You can achieve what you want by creating a class that implements both of the interfaces you need.您可以通过创建一个实现您需要的两个接口的类来实现您想要的。

private class SerializableClickListener implements View.OnClickListener, Serializable {

    @Override public void onClick() {
        // TODO handle click
    }
}

However, just because you can doesn't mean you should.然而,仅仅因为你可以并不意味着你应该。 Sending a click listener to another activity is a horrible code smell, and you should really rethink how you could do this via Intents/Broadcasts.将点击侦听器发送到另一个活动是一种可怕的代码味道,您真的应该重新考虑如何通过 Intents/Broadcasts 来做到这一点。

I tried to pass the OnlclickListener and I couldn't.我试图通过 OnlclickListener 但我不能。 then I tried this solution.然后我尝试了这个解决方案。

  1. I made a static click listener variable in a GlobalData class我在 GlobalData 类中创建了一个静态点击侦听器变量

    public static View.OnClickListener btn;
  2. Then when I call the startactivity to go to another activity I did this.然后,当我调用 startactivity 转到另一个活动时,我这样做了。

     GlobalData.btn = new View.OnClickListener() { @Override public void onClick(View v) { //Listern action } }; c.startActivity(new Intent(c, DialogActivity.class));
  3. Then in the second activity, I can set the static listener reference which I used to assign a listener object in the first activity.然后在第二个活动中,我可以设置用于在第一个活动中分配侦听器对象的静态侦听器引用。

     b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(GlobalData.btn!=null){ GlobalData.btn1.onClick(v); } finish(); } });

I didn't use it directly as a parameter so I can do other stuff in the second activity listener.我没有直接将它用作参数,因此我可以在第二个活动侦听器中执行其他操作。 this worked for me.这对我有用。

But you have to think more because you are using a static reference.但是您必须考虑更多,因为您使用的是静态引用。 this is not a 100% solution.这不是 100% 的解决方案。 but it's worth trying.但值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM