保存单选组 onSavedInstance
save radiogroup onSavedInstance
我在屏幕旋转时保存无线电组状态时遇到问题。
我有两个独立的无线电组,每个无线电组有 2 个单选按钮。
我尝试使用 get,getCheckedRadioButtonId
但它根本不起作用。
我做错了什么以及如何正确保存 radiougroup checked radiobutton?
提前致谢!
编辑:
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;
}
}
});
}
您已经像这样保存了选中的 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 代码片段中丢失了)
所以我尝试通过在调用 super.onRestoreInstanceState()
后将所有 RadioButton
重置为 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()
是我确保有东西要恢复的唯一方法:取消选中 RadioGroup
.
中的每个 RadioButton
@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);
}
}
效果很好,希望对您也有帮助。
我在屏幕旋转时保存无线电组状态时遇到问题。
我有两个独立的无线电组,每个无线电组有 2 个单选按钮。
我尝试使用 get,getCheckedRadioButtonId
但它根本不起作用。
我做错了什么以及如何正确保存 radiougroup checked radiobutton? 提前致谢!
编辑:
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;
}
}
});
}
您已经像这样保存了选中的 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 代码片段中丢失了)
所以我尝试通过在调用 super.onRestoreInstanceState()
后将所有 RadioButton
重置为 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()
是我确保有东西要恢复的唯一方法:取消选中 RadioGroup
.
RadioButton
@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);
}
}
效果很好,希望对您也有帮助。