简体   繁体   English

如何为自定义按钮设置点击监听器?

[英]How to set on click listener for a custom button?

I tried to setup my listener for my custom button class.我试图为我的自定义按钮 class 设置我的监听器。 However, it seems that my listener doesn't map to the View.但是,似乎我的听众对视图没有 map。 Any idea to combine my button view and my custom button?任何想法将我的按钮视图和我的自定义按钮结合起来?

I have my fragment with a custom button on a linear layout XML我的片段在线性布局 XML 上有一个自定义按钮

<LinearLayout>
 ...
<Button>
...
</Button>

<LinearLayout>

I tried to setup a click listener for the button in the fragment at onActivityCreated method.我试图在 onActivityCreated 方法的片段中为按钮设置一个点击监听器。

public class myFragment extend Fragment {

@Override
public void onActivityCreated (){

Button myNewButton = new myButton();
myNewButton = root.findViewByID.(R.id.button);
myNewButton.setOnClickListener(new myButton());

}

}

My custom button class with a setonclicklistener method我的自定义按钮 class 带有 setonclicklistener 方法

public class myButton extend button implements View.onClicklistener {

@Override
public void setOnClickListener (OnClickListener listener){
super.setonClickListener(listener);
}

@Override
public void onClick(View view) {
//action to do after on click
}

}

Try this:尝试这个:

Remove this line from myFragment :myFragment删除这一行:

myNewButton.setOnClickListener(new myButton());

Do this in myButton :myButton中执行此操作:

public class myButton extend button implements View.onClicklistener {

@Override
public void setOnClickListener (OnClickListener listener){
//set like this
super.setonClickListener(this);
}

@Override
public void onClick(View view) {
//action to do after on click
}

}

You seem to be setting another button as the listener for your original button, depending on what actions you hope to do in the listener, this can have unexpected behavior.您似乎将另一个按钮设置为原始按钮的侦听器,具体取决于您希望在侦听器中执行的操作,这可能会产生意外行为。

The easier solution would be to set itself as the listener, Whilst you've implemented View.onClicklistener in your MyButton , you haven't set that as the listener to itself.更简单的解决方案是将自己设置为侦听器,虽然您已经在MyButton中实现View.onClicklistener ,但您还没有将其设置为自己的侦听器。 You'll need to do so in the constructor.您需要在构造函数中这样做。

If you wish to support user set onClickListeners along with the MyButton listener, then you'll need to maintain a listener variable in your MyButton class which you can then explicitly call.如果您希望支持用户设置 onClickListeners 以及MyButton侦听器,那么您需要在MyButton class 中维护一个侦听器变量,然后您可以显式调用该变量。

Finally, use your MyButton in your layout directly, instead of using Button as you currently seem to be doing.最后,直接在您的布局中使用您的MyButton ,而不是像您目前似乎正在做的那样使用Button

Your final MyButton class should be something along the following lines,您最终的MyButton class 应该是以下几行,

public class MyButton extends androidx.appcompat.widget.AppCompatButton implements View.OnClickListener {

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

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnClickListener(this);
    }

    private View.OnClickListener mUserOnClickListener;

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        if (l == this) {
            super.setOnClickListener(l);
        } else {
            mUserOnClickListener = l;
        }
    }

    @Override
    public void onClick(View v) {
        //Your actions
        Toast.makeText(getContext(), "MyButton clicked", Toast.LENGTH_SHORT).show();
        if (mUserOnClickListener != null) {
            mUserOnClickListener.onClick(v);
        }
    }
}

PS - I'd also suggest you go through naming conventions for Java. PS - 我还建议您通过 Java 的命名约定 go。

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

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