下面列出了android.widget.CompoundButton#toggle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Called by R.id.shareViaLinkExpirationSwitch to set or clear the expiration date.
*
* @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkExpirationSwitch
* @param isChecked New switch state.
*/
@Override
public void onCheckedChanged(CompoundButton switchView, boolean isChecked) {
if (!isResumed()) {
// very important, setCheched(...) is called automatically during
// Fragment recreation on device rotations
return;
}
if (isChecked) {
ExpirationDatePickerDialogFragment dialog =
ExpirationDatePickerDialogFragment.newInstance(mFile, -1);
dialog.show(
getActivity().getSupportFragmentManager(),
ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
);
} else {
((FileActivity) getActivity()).getFileOperationsHelper().
setExpirationDateToShareViaLink(mFile, -1);
}
// undo the toggle to grant the view will be correct if the dialog is cancelled
switchView.setOnCheckedChangeListener(null);
switchView.toggle();
switchView.setOnCheckedChangeListener(mOnExpirationDateInteractionListener);
}
/**
* Called by R.id.shareViaLinkPasswordSwitch to set or clear the password.
*
* @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkPasswordSwitch
* @param isChecked New switch state.
*/
@Override
public void onCheckedChanged(CompoundButton switchView, boolean isChecked) {
if (!isResumed()) {
// very important, setCheched(...) is called automatically during
// Fragment recreation on device rotations
return;
}
if (isChecked) {
((FileActivity) getActivity()).getFileOperationsHelper().
requestPasswordForShareViaLink(mFile, false);
} else {
((FileActivity) getActivity()).getFileOperationsHelper().
setPasswordToShareViaLink(mFile, ""); // "" clears
}
// undo the toggle to grant the view will be correct if the dialog is cancelled
switchView.setOnCheckedChangeListener(null);
switchView.toggle();
switchView.setOnCheckedChangeListener(mOnPasswordInteractionListener);
}
/**
* Called by R.id.shareViaLinkSectionSwitch to create or delete a public link.
*
* @param switchView {@link Switch} toggled by the user, R.id.shareViaLinkSectionSwitch
* @param isChecked New switch state.
*/
@Override
public void onCheckedChanged(CompoundButton switchView, boolean isChecked) {
if (!isResumed()) {
// very important, setCheched(...) is called automatically during
// Fragment recreation on device rotations
return;
}
if (isChecked) {
if (mCapabilities != null &&
mCapabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
// password enforced by server, request to the user before trying to create
((FileActivity) getActivity()).getFileOperationsHelper().
requestPasswordForShareViaLink(mFile, true);
} else {
// create without password if not enforced by server or we don't know if enforced;
((FileActivity) getActivity()).getFileOperationsHelper().
shareFileViaLink(mFile, null);
// FileActivtiy#onCreateShareViaLinkOperationFinish still handles the guess of enforcement
// for server in versions previous to OwnCloudVersion#MINIMUM_VERSION_CAPABILITIES_API
}
} else {
((FileActivity) getActivity()).getFileOperationsHelper().
unshareFileViaLink(mFile);
}
// undo the toggle to grant the view will be correct if any intermediate dialog is cancelled or
// the create/delete operation fails
switchView.setOnCheckedChangeListener(null);
switchView.toggle();
switchView.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener);
}
/**
* Toggle value of received {@link CompoundButton} granting that its change listener is not called.
*
* @param compound {@link CompoundButton} (switch or checkBox) to toggle without reporting to
* the change listener
*/
private void toggleDisablingListener(CompoundButton compound) {
compound.setOnCheckedChangeListener(null);
compound.toggle();
compound.setOnCheckedChangeListener(this);
}