我创建了一个自定义视图。我的自定义视图从RelativeLayout
扩展:
public class CircleProgressButton extends RelativeLayout {...
在我的自定义视图中,我有一个按钮:
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);
}
现在我将自定义视图附加到片段布局:
<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"/>
在我的片段中,我像这样绑定片段布局:
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_marhaleh, container, false);
binding.btnSave.setOnClickListener(v->{
Log.i("======", "onSaveClicked: ");
// do my stuff
});
运行应用程序并单击
btnSave
后,logcat上将显示日志。现在,我想在自定义视图中覆盖OnClick,以便按顺序在我的自定义视图中实现
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);
}
触发按钮onclick
binding.btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binding.btnSave.onClick(v);
Log.i("======", "onSaveClicked: ");
//code
}
});
在这之后应该工作