【发布时间】:2017-01-03 20:17:36
【问题描述】:
我想在登录活动中设置默认选中的单选按钮,因为用户登录时活动按钮设置自动选中,当用户进入设置活动然后更改单选按钮选择时,它会保存选择用户在设置活动中选择按钮。
代码:
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();
【问题讨论】:
-
尝试使用 shareprefernce 来存储单选按钮的布尔值。并使用 SharePrefernce 设置为 Log 活动 ....
标签: android android-studio radio-button radio-group