更改屏幕方向时保持微调器的位置

时间:2016-01-26 16:08:32

标签: android spinner

我希望在旋转屏幕时保持微调器的位置

我在这里看了几个答案,到目前为止还有以下

目标 - 存储微调器的位置

protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
    Integer flav1 = flav1Spinner.getSelectedItemPosition();
    savedInstanceState.putInt("cho1", flav1);
}

目标 - 恢复位置

 protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculation);
    Locale.setDefault(Locale.US);
   if (savedInstanceState != null){
        flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
        Integer flav1 = savedInstanceState.getInt("cho1");
        flav1Spinner.setSelection(flav1);
    }

然而,在调试时我可以看到Integer Flav1正在获取一个分配给它的值并且正在检索数字,但是微调器没有改变它的位置。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以onSaveInstanceState()执行此操作,如下所示

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition());
    // do this for each of your Spinner

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
        // do this for every text views
    }
}

检查Saving Activity state,它会帮助您获取更多详细信息。