gpt4 book ai didi

android - 无法使用点击监听器创建自定义按钮

转载 作者:行者123 更新时间:2023-11-30 00:03:25 24 4
gpt4 key购买 nike

我是 Android 的新手,想创建一个自定义的点击监听器,但是当我点击按钮时下面的代码没有任何 Action 。有人可以指导我吗

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);
}
}

最佳答案

下面的代码应该可以工作:

自定义按钮类:----------------

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>

关于android - 无法使用点击监听器创建自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49464564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com