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