Created
July 27, 2018 04:33
-
-
Save Guilherme-HRamos/ebfdfa323bdadfb25a6085f0752dbea9 to your computer and use it in GitHub Desktop.
Simulação de RadioGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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