下面列出了android.widget.CompoundButton#OnCheckedChangeListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void assignBooleanSwitch(final APDE context, final String key, final boolean defaultValue, final SwitchCompat switchCompat, final MutableBoolean value, final CompoundButton.OnCheckedChangeListener listener) {
boolean savedValue = getPreferences(context).getBoolean(key, defaultValue);
value.set(savedValue);
switchCompat.setChecked(savedValue);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
value.set(isChecked);
getPreferences(context).edit().putBoolean(key, value.get()).commit();
if (listener != null) {
listener.onCheckedChanged(buttonView, isChecked);
}
}
});
}
private Switch.OnCheckedChangeListener listener(final int roll) {
return new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (roll == ROLL.READ.ordinal()) {
Extra.getInstance(getBaseContext()).setRead(isChecked);
} else if (roll == ROLL.SEEN.ordinal()) {
Extra.getInstance(getBaseContext()).setSeen(isChecked);
} else if (roll == ROLL.DELIVERED.ordinal()) {
Extra.getInstance(getBaseContext()).setDelivered(isChecked);
} else if (roll == ROLL.SLOT.ordinal()) {
Extra.getInstance(getBaseContext()).setSLOT_ENABLED(isChecked);
} else if (roll == ROLL.IMSI.ordinal()) {
Extra.getInstance(getBaseContext()).setIMSI_ENABLED(isChecked);
} else if (roll == ROLL.REPLY_PATH.ordinal()) {
Extra.getInstance(getBaseContext()).setREPLY_PATH_PRESENT(isChecked);
} else if (roll == ROLL.SERVICE_CENTER.ordinal()) {
Extra.getInstance(getBaseContext()).setServiceCenterBool(isChecked);
} else if (roll == ROLL.TIME_DIFFERENCE.ordinal()) {
Extra.getInstance(getBaseContext()).setTimeDifferenceBool(isChecked);
} else if (roll == ROLL.STATUS.ordinal()) {
Extra.getInstance(getBaseContext()).setSmsStatusBool(isChecked);
}
}
};
}
public Adapter(PreventActivity activity) {
super(activity, R.layout.item);
mActivity = activity;
mPm = mActivity.getPackageManager();
inflater = LayoutInflater.from(activity);
mListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ViewHolder holder = (ViewHolder) buttonView.getTag();
Set<String> selections = mActivity.getSelection();
if (isChecked) {
selections.add(holder.packageName);
} else {
selections.remove(holder.packageName);
}
mActivity.checkSelection();
}
};
}
public static SwitcherFragment newInstance(String title, String summary, boolean checked,
CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
Bundle args = new Bundle();
args.putString(INTENT_TITLE, title);
args.putString(INTENT_SUMMARY, summary);
args.putBoolean(INTENT_CHECKED, checked);
SwitcherFragment fragment = new SwitcherFragment();
fragment.setArguments(args);
fragment.mOnCheckedChangeListener = onCheckedChangeListener;
return fragment;
}
public Card(String title, String desc, String color, String prop, ActionBarActivity fa, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
this.title = title;
this.desc = desc;
this.color = color;
this.fa = fa;
this.prop = prop;
this.onCheckedChangeListener = onCheckedChangeListener;
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.skill_event__timer, container, false);
group_short = view.findViewById(R.id.group_short);
group_long = view.findViewById(R.id.group_long);
radioButton_short = view.findViewById(R.id.radioButton_short);
radioButton_long = view.findViewById(R.id.radioButton_long);
CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView == radioButton_short) {
radioButton_long.setChecked(!isChecked);
group_short.setVisibility(isChecked ? View.VISIBLE : View.GONE);
} else if (buttonView == radioButton_long) {
radioButton_short.setChecked(!isChecked);
group_long.setVisibility(isChecked ? View.VISIBLE : View.GONE);
} else {
throw new IllegalStateException("This OnCheckedChangeListener shouldn't be used elsewhere.");
}
}
};
radioButton_short.setOnCheckedChangeListener(onCheckedChangeListener);
radioButton_long.setOnCheckedChangeListener(onCheckedChangeListener);
radioButton_long.setChecked(true);
radioButton_short.setChecked(true);
editText_second = view.findViewById(R.id.editText_second);
editText_minute = view.findViewById(R.id.editText_minute);
radioButton_exact = view.findViewById(R.id.radioButton_exact);
radioButton_inexact = view.findViewById(R.id.radioButton_inexact);
radioButton_repeat = view.findViewById(R.id.radioButton_repeat);
radioButton_one_time = view.findViewById(R.id.radioButton_one_time);
return view;
}
public Card(String title, String desc, String prop, ActionBarActivity fa, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
this.title = title;
this.desc = desc;
this.fa = fa;
this.prop = prop;
this.onCheckedChangeListener = onCheckedChangeListener;
}
private void setupSettingsSwitch(int containerId, int labelId, int switchId, boolean checked,
CompoundButton.OnCheckedChangeListener checkedChangeListener) {
ViewGroup container = findViewById(containerId);
String switchLabel = ((TextView) container.findViewById(labelId)).getText().toString();
final Switch switchView = container.findViewById(switchId);
switchView.setContentDescription(switchLabel);
switchView.setChecked(checked);
container.setOnClickListener((view) -> switchView.performClick());
switchView.setOnCheckedChangeListener(checkedChangeListener);
}
private void initializeViews(@NonNull View view) {
customNotificationsSwitch = view.findViewById(R.id.custom_notifications_enable_switch);
soundLabel = view.findViewById(R.id.custom_notifications_sound_label);
soundSelector = view.findViewById(R.id.custom_notifications_sound_selection);
vibrateLabel = view.findViewById(R.id.custom_notifications_vibrate_label);
vibrateSwitch = view.findViewById(R.id.custom_notifications_vibrate_switch);
Toolbar toolbar = view.findViewById(R.id.custom_notifications_toolbar);
toolbar.setNavigationOnClickListener(v -> dismissAllowingStateLoss());
CompoundButton.OnCheckedChangeListener onCustomNotificationsSwitchCheckChangedListener = (buttonView, isChecked) -> {
viewModel.setHasCustomNotifications(isChecked);
};
viewModel.isInitialLoadComplete().observe(getViewLifecycleOwner(), customNotificationsSwitch::setEnabled);
viewModel.hasCustomNotifications().observe(getViewLifecycleOwner(), hasCustomNotifications -> {
if (customNotificationsSwitch.isChecked() != hasCustomNotifications) {
customNotificationsSwitch.setOnCheckedChangeListener(null);
customNotificationsSwitch.setChecked(hasCustomNotifications);
}
customNotificationsSwitch.setOnCheckedChangeListener(onCustomNotificationsSwitchCheckChangedListener);
soundLabel.setEnabled(hasCustomNotifications);
vibrateLabel.setEnabled(hasCustomNotifications);
soundSelector.setVisibility(hasCustomNotifications ? View.VISIBLE : View.GONE);
vibrateSwitch.setVisibility(hasCustomNotifications ? View.VISIBLE : View.GONE);
});
if (!NotificationChannels.supported()) {
customNotificationsSwitch.setVisibility(View.GONE);
view.findViewById(R.id.custom_notifications_enable_label).setVisibility(View.GONE);
}
CompoundButton.OnCheckedChangeListener onVibrateSwitchCheckChangedListener = (buttonView, isChecked) -> {
viewModel.setMessageVibrate(isChecked ? RecipientDatabase.VibrateState.ENABLED : RecipientDatabase.VibrateState.DISABLED);
};
viewModel.getVibrateState().observe(getViewLifecycleOwner(), vibrateState -> {
boolean vibrateEnabled = vibrateState != RecipientDatabase.VibrateState.DISABLED;
if (vibrateSwitch.isChecked() != vibrateEnabled) {
vibrateSwitch.setOnCheckedChangeListener(null);
vibrateSwitch.setChecked(vibrateEnabled);
}
vibrateSwitch.setOnCheckedChangeListener(onVibrateSwitchCheckChangedListener);
});
viewModel.getNotificationSound().observe(getViewLifecycleOwner(), sound -> {
soundSelector.setText(getRingtoneSummary(requireContext(), sound));
soundSelector.setTag(sound);
});
soundSelector.setOnClickListener(v -> launchSoundSelector(viewModel.getNotificationSound().getValue()));
}
public T setOnCheckedChangeListener(int id, CompoundButton.OnCheckedChangeListener listener) {
CompoundButton button = getContentView().findViewById(id);
button.setOnCheckedChangeListener(listener);
return back();
}
/**
* @param viewId
* @param listener
* @return
*/
public CommonHolder setCheckedChanged(int viewId, CompoundButton.OnCheckedChangeListener listener) {
View view = getView(viewId);
((CheckBox) view).setOnCheckedChangeListener(listener);
return this;
}
public CardSwitchDisabled(String title, String desc, String color, String prop, ActionBarActivity fa, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
super(title, desc, color, prop, fa, onCheckedChangeListener);
}
@Override
public void setOnCheckedChangeListener(
CompoundButton.OnCheckedChangeListener listener){
if(this.mListener == null) {this.mListener = listener;}
super.setOnCheckedChangeListener(listener);
}
public ItemViewHelper setOnCheckedChangeListener(int viewId, CompoundButton.OnCheckedChangeListener listener) {
CompoundButton view = this.getView(viewId);
view.setOnCheckedChangeListener(listener);
return this;
}
@Override
public void onBindViewHolder(final SaklarHolder holder, final int position) {
final Saklar saklar = saklarList.get(position);
// Saklar Switch Listenner
CompoundButton.OnCheckedChangeListener toggleListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
AgnosthingsApi api = new AgnosthingsApi(context);
realm.beginTransaction();
if (isChecked) {
saklar.setValue(1);
holder.saklarIcon.setImageResource(R.drawable.ic_lightbulb_outline_green);
} else {
saklar.setValue(0);
holder.saklarIcon.setImageResource(R.drawable.ic_lightbulb_outline_grey);
}
realm.commitTransaction();
// Push updated data saklar
api.pushDataSaklar();
}
};
holder.saklarName.setText(saklar.getName());
// Disable Listener sementara
holder.saklarSwitch.setOnCheckedChangeListener(null);
// Set Saklar dari Object / Database
if (saklar.getValue() == 1) {
holder.saklarIcon.setImageResource(R.drawable.ic_lightbulb_outline_green);
holder.saklarSwitch.setChecked(true);
} else if (saklar.getValue() == 0) {
holder.saklarIcon.setImageResource(R.drawable.ic_lightbulb_outline_grey);
holder.saklarSwitch.setChecked(false);
}
holder.saklarSwitch.setOnCheckedChangeListener(toggleListener);
}
public void init(boolean defaultValue, CompoundButton.OnCheckedChangeListener listener) {
binding.settingSwitch.setChecked(defaultValue);
onCheckedChangeListener = listener;
}
public void setOnCheckedChanged(final CompoundButton.OnCheckedChangeListener listener) {
this.listener = listener;
}
public void setOnCheckClick(CompoundButton.OnCheckedChangeListener listener) {
checkBox.setOnCheckedChangeListener(onCheckedChangeListener = listener);
checkBox.setOnClickListener(v -> {
});
}
/**
* {@inheritDoc}
*/
@Override
public void setOnCheckedChangeListener(
@Nullable CompoundButton.OnCheckedChangeListener listener) {
mListener = listener;
}
void setOnCheckedChangeListener(@Nullable CompoundButton.OnCheckedChangeListener listener);