亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android:在 customView 中覆蓋 onClick 時 OnClick 不起作用

Android:在 customView 中覆蓋 onClick 時 OnClick 不起作用

手掌心 2023-05-17 15:00:08
我創建了一個自定義視圖。我的自定義視圖擴展自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);    }
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

試試這個對我有用,在 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

        }

    });

之后應該可以工作


查看完整回答
反對 回復 2023-05-17
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

如果你使用數據綁定試試這個


<com.tazik.circleprogressbutton.CircleProgressButton

 android:onClick="@{()->viewModel.click()}"

 .........../>


查看完整回答
反對 回復 2023-05-17
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號