Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Created July 27, 2018 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guilherme-HRamos/ebfdfa323bdadfb25a6085f0752dbea9 to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/ebfdfa323bdadfb25a6085f0752dbea9 to your computer and use it in GitHub Desktop.
Simulação de RadioGroup
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
* Classe criada por Guilherme Ramos em 2018
*/
public final class RadioGroupSimulateUtils {
/**
* RadioButtons que irão ser utilizados
*/
private final RadioButton[] args;
/**
* Callback para receber o RadioButton clicado
*/
private RadioGroupCallback callback;
/**
* Classe que irá simular o uso de um RadioGroup
* ver {@link RadioGroup}
* @param args RadioButtons usados
*/
public RadioGroupSimulateUtils(RadioButton... args) {
this.args = args;
}
/**
* Configurando o callback que irá receber o resultado da escolha
* @param callback callback para receber o resultado
*/
public final void setupWithCallback(RadioGroupCallback callback) {
this.callback = callback;
init();
}
private void init() {
for (RadioButton arg : args) {
arg.setOnCheckedChangeListener(listener);
}
}
private final CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!buttonView.isChecked())
return;
for (RadioButton arg : args) {
arg.setChecked(false);
}
buttonView.setChecked(true);
callback.onRadioCallback(buttonView.getId());
}
};
public interface RadioGroupCallback {
void onRadioCallback(int optionId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment