简体   繁体   English

Android:在customView中覆盖onClick时,OnClick无法正常工作

[英]Android: OnClick not work when override onClick in customView

I have created a custom view. 我创建了一个自定义视图。 My custom view extended from RelativeLayout : 我的自定义视图从RelativeLayout扩展:

public class CircleProgressButton extends RelativeLayout {...

In my Custom view i have a button : 在我的自定义视图中,我有一个按钮:

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

Now i attached my custom view to fragment layout : 现在我将自定义视图附加到片段布局:

<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"/>

In my fragment i bind fragment layout like this: 在我的片段中,我像这样绑定片段布局:

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

After running application and clicked on btnSave log is appearing on logcat. 运行应用程序并单击btnSave ,logcat上将显示日志。

Now i want to override OnClick in my custom view so in order, in my custom view i implemented View.OnClickListener : 现在,我想在自定义视图中覆盖OnClick,以便按顺序在我的自定义视图中实现View.OnClickListener

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

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

And i override onClick: 而且我覆盖onClick:

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

}

Now after running app when i clicked on btnSave log is not appearing that's mean 现在在运行应用程序后,当我单击btnSave日志时未出现,这意味着

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

not working? 不工作吗? What is happening? 怎么了?

Try this worked for me, in class CircleProgressButton add this funtion: 试试这个对我有用的方法,在CircleProgressButton类中添加以下功能:

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

and call init() on all constructors. 并在所有构造函数上调用init()。 remove this one 删除这个

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

edit this method like this: 像这样编辑此方法:

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

trigger button onclick 触发按钮onclick

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

after this should be work 在这之后应该工作

if you use databinding try this one 如果您使用数据绑定,请尝试此

<com.tazik.circleprogressbutton.CircleProgressButton
 android:onClick="@{()->viewModel.click()}"
 .........../>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM