无法使用单击侦听器创建自定义按钮

时间:2018-03-24 12:15:06

标签: android android-custom-view

我是Android新手,想要创建自定义点击监听器,但是当我点击按钮时,下面的代码不会导致任何操作。有人请指导我

MainActivity代码:

public class MainActivity extends AppCompatActivity {
    CustomButton button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.custombutton);
        button.setClickListener(new CustomButton.OnClickListener() {
            @Override
            public void onClick(View var1) {
                Toast.makeText(MainActivity.this, "SGB Custom Button", Toast.LENGTH_SHORT).show();
            }
        });


    }

}

自定义按钮代码:

public class CustomButton extends android.support.v7.widget.AppCompatButton {
    public OnClickListener event;

    public CustomButton(Context context) {
        super(context);
    }

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

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


    public void setClickListener(OnClickListener event) {
        this.event = event;
    }

    public interface OnClickListener {
        public void onClick(View var1);
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码应该有效:

customButton类:-----------------

public class customButton extends AppCompatButton implements View.OnClickListener {

private CustomOnClickListener event;
private Context mContext;

public customButton(Context context) {
    super(context);
}

public customButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    setOnClickListener(this);
}

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


@Override
public void onClick(View v) {
    event.onClick(v);
}

public void setCustomClickListener(CustomOnClickListener event) {
    this.event = event;
}

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

MainActivity.class:----------------

public class MainActivity extends AppCompatActivity{
customButton button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.customButton);
    button.setCustomClickListener(new customButton.CustomOnClickListener() {
        @Override
        public void onClick(View var1) {
            Toast.makeText(MainActivity.this, "SGB Custom Button", Toast.LENGTH_SHORT).show();
        }
    });
    }

}

activity_main.xml中:-----------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.admin.accessories.customButton
    android:layout_width="match_parent"
    android:text="Button"
    android:layout_height="wrap_content"
    android:id="@+id/customButton"/>

</android.support.constraint.ConstraintLayout>