屏幕旋转时,我在保存无线电组状态时遇到问题。
我有两个独立的无线电组,每个无线电组有2个单选按钮。
我尝试使用get getCheckedRadioButtonId
,但它根本不起作用。
我在做什么错,如何正确保存radiougroup检查的单选按钮? 预先感谢!
编辑:
xml:
<RadioGroup
android:id="@+id/radio_result_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="@+id/convert_to3"
app:layout_constraintTop_toTopOf="@+id/textView">
<RadioButton
android:id="@+id/metric_sys_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/metric_sys"
android:textSize="14sp" />
<RadioButton
android:id="@+id/imperial_sys_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/imperial_sys"
android:textColorLink="@color/colorAccent"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@+id/measurementView" />
</RadioGroup>
<RadioGroup
android:id="@+id/radio_value_gropu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/measurementView"
app:layout_constraintEnd_toEndOf="@+id/convert_from5"
app:layout_constraintTop_toTopOf="@+id/measurementView">
<RadioButton
android:id="@+id/metric_sys_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/metric_sys"
android:textSize="14sp" />
<RadioButton
android:id="@+id/imperial_sys_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/imperial_sys"
android:textColorLink="@color/colorAccent"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@+id/measurementView" />
</RadioGroup>
// java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
protected double getEnteredValue;
protected SpinnerAdapter checkAdapter;
protected int pos;
protected ArrayAdapter<String> adapter 1, adapter2, adapter3,
adapter4;
@BindView(R.id.unit_From_spinner)Spinner spinnerFrom;
@BindView(R.id.unit_To_spinner)Spinner spinnertTo;
@BindView(R.id.radio_result_group) RadioGroup resultGroup;
@BindView(R.id.radio_value_gropu) RadioGroup valueGroup;
@BindView(R.id.valueEditTextView) EditText valueEdit;
@BindView(R.id.resultView) TextView resultView;
@BindView(R.id.conver_button) Button convertButton;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("result",resultView.getText().toString());
savedInstanceState.putString("value",valueEdit.getText().toString());
savedInstanceState.putInt("from",spinnerFrom.getSelectedItemPosition()); // get selected item from spinner 1
savedInstanceState.putInt("to",spinnertTo.getSelectedItemPosition());
savedInstanceState.putInt("radGroup1", resultGroup.getCheckedRadioButtonId());
savedInstanceState.putInt("radGroup2", valueGroup.getCheckedRadioButtonId());
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unifiedlayout);
ButterKnife.bind(this);
convertButton.setEnabled(false);
adapter1 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray( R.array.adapter_1) );
adapter2 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array. adapter_2));
adapter3 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray( R.array. adapter_3) );
adapter4 = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array. adapter_4));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
resultView.setText(savedInstanceState.getString("result"));
valueEdit.setText(savedInstanceState.getString("value"));
spinnerFrom.setSelection(savedInstanceState.getInt("from"));
spinnertTo.setSelection(savedInstanceState.getInt("to"));
//?
super.onSaveInstanceState(savedInstanceState);
}
@OnClick({R.id.button1, R.id.button2,R.id.button3,R.id.button4
} )
public void setViewOnClickEvent(View view){
switch(view.getId()){
case R.id.mass_button:
fillSpinner(adapter1, adapter2); // fill spinner with values
break;
case R.id.speed_button:
fillSpinner(adapter3,
adapter4);
break;
}
}
protected void fillSpinner (final ArrayAdapter<String> spinnerToAdapter,
final ArrayAdapter<String> spinnerFromAdapter){
valueGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup valueGroup, int checkedId) {
pos = valueGroup.indexOfChild(findViewById(checkedId));
switch (pos) {
case 0:
spinnerFrom.setAdapter(spinnerToAdapter);
break;
case 1:
spinnerFrom.setAdapter(spinnerFromAdapter);
break;
}
}
});
resultGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup resultGroup, int checkedId) {
pos = resultGroup.indexOfChild(findViewById(checkedId));
switch (pos) {
case 0:
spinnertTo.setAdapter(spinnerToAdapter);
break;
case 1:
spinnertTo.setAdapter(spinnerFromAdapter);
break;
}
}
});
}
答案 0 :(得分:1)
您已经像这样保存了选中的RadioButton
的ID:
@Override
protected void onSaveInstanceState(Bundle outState) {
//
// saving other values...
//
outState.putInt("radGroup1", resultGroup.getCheckedRadioButtonId());
outState.putInt("radGroup2", valueGroup.getCheckedRadioButtonId());
Log.d(TAG, "onSaveInstanceState: result id " + resultGroup.getCheckedRadioButtonId() + ", value id = " + valueGroup.getCheckedRadioButtonId());
super.onSaveInstanceState(outState);
}
因此,您可以在onCreate()
(注意:仅if(savedInstanceState != null)
)或onRestoreInstanceState()
中检索保存的值。仅当 savedInstanceState 不是null
时,才会调用第二种方法。
private int checkedResultButtonsId;
private int checkedValueButtonsId;
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
checkedResultButtonsId = savedInstanceState.getInt("radGroup1");
checkedValueButtonsId = savedInstanceState.getInt("radGroup2");
Log.d(TAG, "onRestoreInstanceState: result id " + checkedResultButtonsId + ", value id = " + checkedValueButtonsId);
}
通常,在配置更改后,您无需手动设置RadioButton
的选中状态。我的示例应用程序(只是普通的android系统,没有数据绑定)会自动保持RadioButton
的状态,所以我不确定为什么需要像您一样保存id。您的问题听起来像RadioGroup
在旋转时失去状态。
(也许您只是不调用super.onRestoreInstanceState()
?至少在您的MainActivity代码段中缺少它)
因此,我尝试通过在调用RadioButton
之后将所有super.onRestoreInstanceState()
重置为 unchecked 来模拟这种效果,并使用保存的值恢复状态:
@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); resetRadioButtons(resultGroup); resetRadioButtons(valueGroup); checkedResultButtonsId = savedInstanceState.getInt("radGroup1"); checkedValueButtonsId = savedInstanceState.getInt("radGroup2"); // if saved values are > 0: set checked state to 'true' if(checkedResultButtonsId > 0){ ((RadioButton)resultGroup.findViewById(checkedResultButtonsId)).setChecked(true); } if(checkedResultButtonsId > 0){ ((RadioButton)valueGroup.findViewById(checkedValueButtonsId)).setChecked(true); } }
结果有点奇怪:恢复的RadioButton
似乎停留在未检查的状态,只有在检查了它们的同级RadioButton
后才恢复。
我猜我的代码段以某种方式干扰了super.onRestoreInstanceState()
的工作。因此,我将有问题的部分移至onStart()
:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
checkedResultButtonsId = savedInstanceState.getInt("radGroup1");
checkedValueButtonsId = savedInstanceState.getInt("radGroup2");
}
注意:resetRadioButtons()
是确保要还原的唯一方法:取消选中RadioButton
中的每个RadioGroup
。
@Override
protected void onStart() {
super.onStart();
resetRadioButtons(resultGroup);
resetRadioButtons(valueGroup);
if(checkedResultButtonsId > 0){
((RadioButton)resultGroup.findViewById(checkedResultButtonsId)).setChecked(true);
}
if(checkedResultButtonsId > 0){
((RadioButton)valueGroup.findViewById(checkedValueButtonsId)).setChecked(true);
}
}
这很好用,所以希望对您也有帮助。