当屏幕旋转时,我在保存组状态时遇到问题。
我有两个独立的放射组,每个放射组有两个单选按钮。我试着用get, getCheckedRadioButtonId
但根本不起作用。
我做错了什么?如何正确保存radiougroup checked单选按钮?提前谢谢!
编辑:
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;
}
}
});
}
1条答案
按热度按时间iq3niunx1#
您已经保存了选中对象的id
RadioButton
是这样的:因此,您可以在
onCreate()
(注:仅限if(savedInstanceState != null)
)或在onRestoreInstanceState()
. 第二个方法只有在savedinstancestate不是null
.通常不需要设置
RadioButton
配置更改后手动执行。我的示例应用程序(只是普通的android,没有数据绑定)保持RadioButton
是自动的,所以我不知道你为什么要像你一样保存身份证。你的问题听起来像RadioGroup
旋转时失去状态。(也许你就是不打电话
super.onRestoreInstanceState()
? 至少在mainactivity代码段中缺少它)所以我试图通过重置所有
RadioButton
在调用super.onRestoreInstanceState()
并使用保存的值还原状态:结果有点奇怪:恢复了
RadioButton
s似乎被困在一个未经检查的状态,只有在检查他们的兄弟姐妹恢复RadioButton
.我想我的代码片段可能干扰了
super.onRestoreInstanceState()
. 所以我把有问题的部分移到onStart()
:注:
resetRadioButtons()
这是我确保有东西要恢复的唯一方法:取消选中RadioButton
在RadioGroup
.这工作很好,所以我希望它对你也有帮助。