Android:在 customView 中覆盖 onClick 时 OnClick 不起作用
Android: OnClick not work when override onClick in customView
我创建了自定义视图。我的自定义视图扩展自 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
});
不工作?发生了什么事?
如果你使用数据绑定试试这个
<com.tazik.circleprogressbutton.CircleProgressButton
android:onClick="@{()->viewModel.click()}"
.........../>
试试这个对我有用,在 class 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
}
});
之后应该可以工作了
我创建了自定义视图。我的自定义视图扩展自 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
});
不工作?发生了什么事?
如果你使用数据绑定试试这个
<com.tazik.circleprogressbutton.CircleProgressButton
android:onClick="@{()->viewModel.click()}"
.........../>
试试这个对我有用,在 class 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
}
});
之后应该可以工作了