开发者

android- multi onClick listener in one button

开发者 https://www.devze.com 2023-04-08 04:30 出处:网络
I have made a custom component like Mybutton.java and I have set an onclick listener in Mybutton.java.

I have made a custom component like Mybutton.java and I have set an onclick listener in Mybutton.java.

Now, in my new activity, I have to call a Mybutton and add content in onclick listener.

However, if I use OnClickListener mClickListener = new OnClickListener(){...... it will replace the old content. I hope it can do the old and new listener together.

I have searched for some information, found out i can implement this method. After many attempts, I'm still getting errors.

Can anyone give me a simple example that i can learn to modify it开发者_高级运维?


I don't think there's an API in the Android API that allows multiple onClick listeners. You'd need some custom class that handles a single onClick() and pass in handlers for it to call. Something like this:

private class CompositeOnClickListener implements View.OnClickListener{
    List<View.OnClickListener> listeners;

    public CompositeOnClickListener(){
        listeners = new ArrayList<View.OnClickListener>();
    }

    public void addOnClickListener(View.OnClickListener listener){
        listeners.add(listener);
    }

    @Override
    public void onClick(View v){
       for(View.OnClickListener listener : listeners){
          listener.onClick(v);
       }
    }
}

When your setting your buttons, do:

CompositeOnClickListener groupListener = new CompositeOnClickListener();
myButton.setOnClickListener(groupListener);

Then, whenever you want to add another listener, just call

groupListener.addOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v){
      **** Custom implementation ****
   }
});


You could create your custom Button class something like this :

    public class MyButton extends Button {

    private CustomOnClickListener mCustomOnClickListener;

    public interface CustomOnClickListener {
        void onClick(View v);
    }


    public MyButton(Context context) {
        super(context);

        // Set your own onClickListener
        View.OnClickListener ocl = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do whatever you want to do

                // Invoke the other added onclick listener
                if(mCustomOnClickListener != null) {
                    mCustomOnClickListener.onClick(v);
                }
            }
        };
        setOnClickListener(ocl);
    }

    // use this function to set the other onclick listener
    public void setCustomOnClickListener(CustomOnClickListener cl) {
        mCustomOnClickListener = cl;
    }

    }

and, use it like this :

    // create your button
    MyButton button = new MyButton(context);
    // add your custom onClickListener
    button.setCustomOnClickListener(new MyButton.CustomOnClickListener() {
        @Override
        public void onClick(View v) {
            // Do whatever you intend to do after the actual onClickListener
            // from MyButton class has been invoked.
        }
    });


If you want to execute some internal logic in your custom view's onClick and want to execute the externally set up OnClickListener's logic, I think a simple way is overriding setOnClickListener as below.

In Kotlin:

override fun setOnClickListener(externalOnClickListener: View.OnClickListener?) {
    val internalOnClickListener = View.OnClickListener { view -> 
        //Your awesome internal logic
        externalOnClickListener?.onClick(view)
    }
    super.setOnClickListener(internalOnClickListener)
}

Same in Java:

@Override
public void setOnClickListener(@Nullable final View.OnClickListener externalOnClickListener) {
    View.OnClickListener internalOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            //Your awesome internal logic
            if (externalOnClickListener != null) {
                externalOnClickListener.onClick(view);
            }
        }
    };
    super.setOnClickListener(internalOnClickListener);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消