To work with CheckBox, in your layout create a CheckBox:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seu CheckBox"/>
In your MainActivity, instantiate the object:
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.SEU_LAYOUT_XML);
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Se estiver selecionado então mostre a mensagem de boas vindas
if (checkBox.isChecked())
benVindo();
}
});
}
private void bemVindo() {
Toast.makeText(this, "Seja bem vindo", Toast.LENGHT_SHORT).show();
}
}
As for your doubt about leaving the method coming on in this class or another class, go to your preference (Business rule), if you want the method strong> is in another class, you should call it this way:
public class Configuracoes {
public static void bemVindo(Context context) {
Toast.makeText(context, "Seja bem vindo", Toast.LENGHT_SHORT).show();
}
}
Now in your MainActivity:
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.SEU_LAYOUT_XML);
checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Se estiver selecionado então mostre a mensagem de boas vindas
if (checkBox.isChecked())
Configuracoes.bemVindo(MainActivity.this);
}
});
}
}
To save CheckBox's status, use SharedPreferences like this:
private void salvar(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean carregar() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
// Você pode associar esse método ao click do botão salvar por exemplo, ou caso não queira
private void salvarStatusCheckBox() {
save(checkBox.isChecked());
}
// No onResume você carrega o sharedPreferences
@Override
public void onResume() {
super.onResume();
checkBox.setChecked(load());
}