gpt4 book ai didi

java - Android:在自定义 View 中覆盖 onClick 时 OnClick 不起作用

转载 作者:行者123 更新时间:2023-12-02 09:37:31 29 4
gpt4 key购买 nike

我创建了一个自定义 View 。我的自定义 View 扩展自 RelativeLayout:

public class CircleProgressButton extends RelativeLayout {...

在我的自定义 View 中,我有一个按钮:

private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.ProgressButton, 0, 0
);
try {
progressHeight = typedArray.getInt(R.styleable.ProgressButton_progress_height, progressHeight);
progressWidth = typedArray.getInt(R.styleable.ProgressButton_progress_width, progressWidth);
progressIconSuccess = typedArray.getResourceId(R.styleable.ProgressButton_progress_iconSuccess, R.drawable.ic_done);
progressIconFail = typedArray.getResourceId(R.styleable.ProgressButton_progress_iconfail, R.drawable.ic_fail);
progressText = typedArray.getString(R.styleable.ProgressButton_progress_text);
progressBackgroundImage = typedArray.getDrawable(R.styleable.ProgressButton_progress_button_background);
} finally {
typedArray.recycle();
}

initButton();
initProgressBar();
initImageView();
}
private void initButton() {
button = new AppCompatButton(getContext());
LayoutParams button_params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
button_params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
button.setLayoutParams(button_params);
button.setText(TextUtils.isEmpty(progressText) ? "Button" : progressText);
button.setGravity(Gravity.CENTER);
StateListDrawable background = new StateListDrawable();
background.addState(StateSet.WILD_CARD, progressBackgroundImage);
// background.addState(new int[]{android.R.attr.state_pressed}, progressBackgroundImagePressed);
button.setBackground(background);
button.setClickable(false);
/* button.setOnClickListener(v -> {
if (imageView.getVisibility() == VISIBLE) imageView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
});*/
addView(button);
}

现在我将自定义 View 附加到 fragment 布局:

<com.tazik.circleprogressbutton.CircleProgressButton
android:id="@+id/btn_save"
android:layout_width="150dp"
android:layout_height="70dp"
app:progress_height="30"
app:progress_width="30"
app:progress_text = "@string/save_btn"
app:progress_iconfail="@drawable/ic_fail"
app:progress_iconSuccess="@drawable/ic_done"
app:progress_button_background="@drawable/mybutton"
android:clickable="true"/>

在我的 fragment 中,我像这样绑定(bind) fragment 布局:

binding = DataBindingUtil.inflate(inflater, R.layout.fragment_marhaleh, container, false);
binding.btnSave.setOnClickListener(v->{
Log.i("======", "onSaveClicked: ");
// do my stuff
});

运行应用程序并单击 btnSave 后,logcat 上将显示日志。

现在我想在我的自定义 View 中重写 OnClick,因此按顺序,在我的自定义 View 中我实现了 View.OnClickListener :

public class CircleProgressButton extends RelativeLayout implements View.OnClickListener {
private OnClickListener listener;

@Override
public void setOnClickListener(@Nullable View.OnClickListener l) {
listener = l;
}

我重写了 onClick:

@Override
public void onClick(View v) {
if(listener != null){
if (imageView.getVisibility() == VISIBLE) imageView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
}

}

现在,当我点击 btnSave 运行应用程序后,日志没有出现,这意味着

 binding.btnSave.setOnClickListener(v->{
Log.i("======", "onSaveClicked: ");
// do my stuff
});

不工作?发生了什么事?

最佳答案

尝试这对我有用,在 CircleProgressButton 类中添加此功能:

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

并在所有构造函数上调用 init() 。删除这个

@Override
public void setOnClickListener(@Nullable View.OnClickListener l) {
listener = l;
}

像这样编辑这个方法:

@Override
public void onClick(View v) {
if (imageView.getVisibility() == VISIBLE) imageView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
}

点击触发按钮

binding.btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binding.btnSave.onClick(v);
Log.i("======", "onSaveClicked: ");
//code
}
});

这之后应该是工作

关于java - Android:在自定义 View 中覆盖 onClick 时 OnClick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373584/

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