我想在登录活动中默认选中单选按钮作为用户登录,此时自动选中活动按钮设置,当用户进入设置活动然后更改单选按钮选择时,它保存了选择用户在设置活动中选择按钮。
代码:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// String radiodefault=R.id.Active;
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Active) {
Toast.makeText(getApplicationContext(), "choice: Active",
Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.Inactive)
{
Toast.makeText(getApplicationContext(), "choice: Inactive",
Toast.LENGTH_SHORT).show();
}
}
});
active = (RadioButton) findViewById(R.id.Active);
inactive = (RadioButton) findViewById(R.id.Inactive);
button = (Button)findViewById(R.id.chooseBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
// find which radioButton is checked by id
if(selectedId == active.getId()) {
Toast.makeText(Settings.this,"Active is selected",Toast.LENGTH_LONG).show();
}
else if(selectedId == inactive.getId())
{
Toast.makeText(Settings.this,"Inactive is selected",Toast.LENGTH_LONG).show();
答案 0 :(得分:0)
您可以使用SharedPreference
存储CheckBox
的值,然后在重新启动activity
时再次加载它。
SharedPreferences preferences = getSharedPreferences("checkBox",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Active",active.isChecked());
editor.putBoolean("InActive",inactive.isChecked());
editor.apply();
然后在加载活动时将其检索为:
boolean isActive = preferences.getBoolean("Active",false);
boolean isInActvie = preferences.getBoolean("InActive",false);
然后,您可以设置CheckBox
的当前状态,如:
active.setChecked(isActive);
inactive.setChecked(isInActive);
答案 1 :(得分:0)
使用SharedPreference存储已检查为默认值