【问题标题】:Android Extended Button detect onClickAndroid扩展按钮检测onClick
【发布时间】:2018-05-19 11:11:30
【问题描述】:

我已扩展 Button 类,但未在扩展按钮中实现 onClick。我在每个地方都使用扩展按钮,并在每个活动中设置onClick 侦听器。

如何检测扩展类中的按钮被点击。

public class ButtonExtended extends Button {
    public ButtonExtended(Context context) {
        super(context);
    }

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

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    //onClick(){ here i want to detect the click }
}

在这个ButtonExtended 类中,我想检测是否只是为了记录目的而单击按钮。我怎样才能做到这一点?

【问题讨论】:

    标签: java android onclicklistener android-button


    【解决方案1】:

    您可以覆盖performClick() 行为以获取点击状态。

    public class ButtonExtended extends AppCompatButton{
    public ButtonExtended(Context context) {
        super(context);
    }
    public ButtonExtended(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean performClick() {
        Log.d("Button","performClick");
        return super.performClick();
    }
    }
    

    performClick() 将在侦听器之前首先被调用。不要在方法 performClick() 中省略 return super.performClick() 这会消耗事件并且不会调用您的侦听器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多