下面列出了android.widget.CompoundButton#isChecked ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获取 CompoundButton text
*
* @param view view
* @return CompoundButton 显示的内容
*/
private static String getCompoundButtonText(View view) {
try {
CompoundButton switchButton = (CompoundButton) view;
Method method;
if (switchButton.isChecked()) {
method = view.getClass().getMethod("getTextOn");
} else {
method = view.getClass().getMethod("getTextOff");
}
return (String) method.invoke(view);
} catch (Throwable ignore) {
ExceptionUtil.exceptionThrow(ignore);
return "UNKNOWN";
}
}
/**
* {@link CompoundButton.OnCheckedChangeListener}
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && !mTag) {
mTag = true;
for (CompoundButton view : mViewSet) {
if (view != buttonView && view.isChecked()) {
// 这个 API 会触发监听事件
view.setChecked(false);
}
}
if (mListener != null) {
mListener.onCheckedChanged((RadioButton) buttonView, buttonView.getId());
}
mTag = false;
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof CompoundButton) {
final CompoundButton button = (CompoundButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
int currentCheck = getExclusiveCheckedId();
if (mExclusive && currentCheck != View.NO_ID) {
setCheckedStateForView(currentCheck, false);
}
mProtectFromCheckedChange = false;
addCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
/**
* Sync value of "can edit" {@link Switch} according to a change in one of its subordinate checkboxes.
*
* If all the subordinates are disabled, "can edit" has to be disabled.
*
* If any subordinate is enabled, "can edit" has to be enabled.
*
* @param subordinateCheckBoxView Subordinate {@link CheckBox} that was changed.
* @param isChecked 'true' iif subordinateCheckBoxView was checked.
*/
private void syncCanEditSwitch(View subordinateCheckBoxView, boolean isChecked) {
CompoundButton canEditCompound = (CompoundButton) getView().findViewById(R.id.canEditSwitch);
if (isChecked) {
if (!canEditCompound.isChecked()) {
toggleDisablingListener(canEditCompound);
}
} else {
boolean allDisabled = true;
for (int i=0; allDisabled && i<sSubordinateCheckBoxIds.length; i++) {
allDisabled &=
sSubordinateCheckBoxIds[i] == subordinateCheckBoxView.getId() ||
!((CheckBox) getView().findViewById(sSubordinateCheckBoxIds[i])).isChecked()
;
}
if (canEditCompound.isChecked() && allDisabled) {
toggleDisablingListener(canEditCompound);
for (int i=0; i<sSubordinateCheckBoxIds.length; i++) {
getView().findViewById(sSubordinateCheckBoxIds[i]).setVisibility(View.GONE);
}
}
}
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof CompoundButton) {
final CompoundButton button = (CompoundButton) child;
if (button.isChecked()) {
mProtectFromCheckedChange = true;
int currentCheck = getExclusiveCheckedId();
if (mExclusive && currentCheck != View.NO_ID) {
setCheckedStateForView(currentCheck, false);
}
mProtectFromCheckedChange = false;
addCheckedId(button.getId());
}
}
super.addView(child, index, params);
}
/**
*
* @param compoundButton changes the text of the given {@link CompoundButton}
* according if it's "on" or "off"
*/
public void setTextAccordingToState(CompoundButton compoundButton)
{
if(compoundButton.isChecked())
{
compoundButton.setText(onText);
}
else if(compoundButton.isChecked() == false)
{
compoundButton.setText(offText);
}
}
/**
* 取消选中
*/
public void clearCheck() {
for (CompoundButton view : mViewSet) {
if (view.isChecked()) {
view.setChecked(false);
}
}
}
/**
* Constructs a CompoundButtonCheckedProperty for a {@link CompoundButton}.
*
* @param button
* the button being bound.
*/
public CompoundButtonCheckedProperty(CompoundButton button) {
final WeakReference<CompoundButton> weakButton = new WeakReference<CompoundButton>(button);
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CompoundButtonCheckedProperty.this.trackable.updateTrackers();
}
});
this.getter = new Function<Boolean>() {
@Override
public Boolean evaluate() {
CompoundButton button = weakButton.get();
if (button != null) {
CompoundButtonCheckedProperty.this.trackable.track();
return CompoundButtonCheckedProperty.this.lastValue = button.isChecked();
} else {
return CompoundButtonCheckedProperty.this.lastValue;
}
}
};
this.setter = new Action<Boolean>() {
@Override
public void invoke(Boolean parameter) {
CompoundButton button = weakButton.get();
if (button != null) {
button.setChecked(parameter);
CompoundButtonCheckedProperty.this.lastValue = parameter;
}
}
};
this.propertyType = Boolean.TYPE;
}
/**
* 获取 CompoundButton text
*
* @param view view
* @return CompoundButton 显示的内容
*/
public static String getCompoundButtonText(View view) {
try {
CompoundButton switchButton = (CompoundButton) view;
Method method;
if (switchButton.isChecked()) {
method = view.getClass().getMethod("getTextOn");
} else {
method = view.getClass().getMethod("getTextOff");
}
return (String) method.invoke(view);
} catch (Exception ex) {
return "UNKNOWN";
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
CompoundButton cb = (CompoundButton) group.findViewById(checkedId);
if(cb!=null && cb.isChecked()){
TextView titleTv = ((TextView)group.findViewById(android.R.id.title));
if(titleTv!=null){
titleTv.setText("selected:"+cb.getText());
}
}
}
@OnClick(R.id.sw_prayer)
void onPrayerChanged(CompoundButton v) {
mPrayerEnabled = v.isChecked();
mPrayerTextView.setEnabled(mPrayerEnabled);
mNotificationCheckBox.setEnabled(mPrayerEnabled);
mVibrateCheckBox.setEnabled(mPrayerEnabled);
mReminderButton.setEnabled(mPrayerEnabled);
mNotificationButton.setEnabled(mPrayerEnabled);
}
public void setCompoundButton(CompoundButton cb) {
if (mCompoundButton == cb) {
return;
}
boolean checked = cb.isChecked();
mCompoundButton.setOnCheckedChangeListener(null);
mCompoundButton = cb;
mCompoundButton.setOnCheckedChangeListener(this);
setChecked(checked);
}
/**
* Determines where to position dividers between children. Note: this is an 'illegal' override
* of a hidden method.
*
* @param childIndex Index of child to check for preceding divider
* @return true if there should be a divider before the child at childIndex
*/
protected boolean hasDividerBeforeChildAt(int childIndex) {
final CompoundButton child = (CompoundButton) getChildAt(childIndex);
if (child == null)
return false;
if (child.getVisibility() == GONE)
return false;
final CompoundButton previous = getVisibleViewBeforeChildAt(childIndex);
if (previous == null)
return false;
// If both are checked, add a divider
return child.isChecked() && previous.isChecked();
}
private void updateDoneButtonState() {
if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
mButtonLayout.updateValidity(true);
return;
}
if (mInterval.getText().toString().length() == 0) {
mButtonLayout.updateValidity(false);
return;
}
if (mEndCount.getVisibility() == View.VISIBLE &&
mEndCount.getText().toString().length() == 0) {
mButtonLayout.updateValidity(false);
return;
}
if (mModel.freq == RecurrenceModel.FREQ_WEEKLY) {
for (CompoundButton b : mWeekByDayButtons) {
if (b.isChecked()) {
mButtonLayout.updateValidity(true);
return;
}
}
mButtonLayout.updateValidity(false);
return;
}
mButtonLayout.updateValidity(true);
}
private void updateDoneButtonState() {
if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
mDone.setEnabled(true);
return;
}
if (mInterval.getText().toString().length() == 0) {
mDone.setEnabled(false);
return;
}
if (mEndCount.getVisibility() == View.VISIBLE &&
mEndCount.getText().toString().length() == 0) {
mDone.setEnabled(false);
return;
}
if (mModel.freq == RecurrenceModel.FREQ_WEEKLY) {
for (CompoundButton b : mWeekByDayButtons) {
if (b.isChecked()) {
mDone.setEnabled(true);
return;
}
}
mDone.setEnabled(false);
return;
}
mDone.setEnabled(true);
}
@Override
public boolean isChecked() {
CompoundButton view = (CompoundButton) getAndWaitForView();
return view.isChecked();
}
@BindingAdapter(value = {"checked"})
public static void setChecked(CompoundButton view, boolean checked) {
if (view.isChecked() != checked) {
view.setChecked(checked);
}
}
public static boolean getBoolean(CompoundButton view) {
return view.isChecked();
}
public boolean getBoolean(int viewId) {
CompoundButton button = findView(viewId);
return button.isChecked();
}
public void onWifiToggle(View v) {
CompoundButton btn = (CompoundButton)v;
wifiTools.setWifiEnabled(btn.isChecked());
if (!btn.isChecked())
setAuthenticateNowEnabled(false, R.string.auth_now_disabled_label);;
}