其他分享
首页 > 其他分享> > Android SwitchCompat和checkbox

Android SwitchCompat和checkbox

作者:互联网

checkbox和SwitchCompat一样的
在这里插入图片描述

     <androidx.appcompat.widget.SwitchCompat
           android:id="@+id/swi_photo"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="end|center_vertical"
           android:layout_marginEnd="8dp" />

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

    mCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                if (tvPrice.length() == 0) {
                    btnPrice.performClick();//模拟btnprice控件点击
                }
            } else {
                tvPrice.setText(null);
                mCheckBox.setChecked(false);
            }
        });

在这里插入图片描述

    private void releaseRequest(String thumb) {
        int isPrivate = mCheckBox.isChecked() ? 1 : 0;//判断是否有点击
        int coin = CastUtil.parseInt(tvPrice.getText().toString().trim());//获取控件上的数据然后强转
        OkHttp.create(this).postPhoto(String.valueOf(thumb), isPrivate, coin).enqueue((call, httpRes) -> {
            ToastUtil.out(httpRes.getMsg());
            mProgressDialog.dismiss();
            if (httpRes.isSuccessful()) {
                EventBus.getDefault().post(new MyPhotoEvent(MyPhotoEvent.ACTION_THUMB));
                finish();
            }
        });
    }

在这里插入图片描述

    private void selectPhotoPrice(View view) {//点击事件后,拉起弹窗,设置回调
        if (mPhotoPricePickerDialog == null) {//做全局变量防止重复创建对象,如果為空时才初始化对象
            mPhotoPricePickerDialog = new PhotoPricePickerDialog()
                    .setCallback(fee -> {
                        tvPrice.setText(String.valueOf(fee.getCoin()));
                        mCheckBox.setChecked(true);
                    })
                    .setOnDismissListener(dialog -> {
                        if (tvPrice.length() == 0) mCheckBox.setChecked(false);
                    });
        }
        mPhotoPricePickerDialog.show(this);
    }

标签:tvPrice,checkbox,String,setChecked,httpRes,isChecked,Android,mCheckBox,SwitchCom
来源: https://blog.csdn.net/weixin_44911775/article/details/123087772