如何在菜单上保存所选单选按钮的实例状态

时间:2015-05-11 20:33:36

标签: android savestate android-optionsmenu

我的工具栏中有radibutton项目的选项菜单:

.knuth-style, .knuth-style:after, .knuth-style:before 
.knuth-style *, .knuth-style *:after, .knuth-style *:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

我希望在onSaveInstanceState,onRestoreInstanceState中发生方向更改时恢复选定的radiobutton,但我不能理解如何从选项菜单中的radiogroup中获取所选按钮。

3 个答案:

答案 0 :(得分:5)

这是一个完全正常运行且经过测试的示例。使用此代码后,无论您旋转屏幕多少次,当前所选项目都将保持不变。

首先,创建这些实例变量以跟踪菜单的状态,并为您将在Bundle中保存的首选项命名:

private final static String MENU_SELECTED = "selected";
private int selected = -1;
MenuItem menuItem;

saveInstanceState()方法应该保存当前选定的值:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt(MENU_SELECTED, selected);
    super.onSaveInstanceState(savedInstanceState);
}

更新onOptionsItemSelected()中当前选定的项目:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        Log.d("settings", "id: " + id);
        return true;
    }

    if (id == R.id.map_terrain){
        Log.d("menuitem", "terrain id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    if (id == R.id.map_normal){
        Log.d("menuitem", "normal id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    if (id == R.id.map_hybrid){
        Log.d("menuitem", "hybrid id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

onCreate()中,加载已保存的数据(如果存在):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null){
        selected = savedInstanceState.getInt(MENU_SELECTED);
    }
}

然后在onCreateOptionsMenu()中重新选择之前选择的项目:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    if (selected == -1){
        return true;
    }

    switch (selected){
        case R.id.map_terrain:
            menuItem = (MenuItem) menu.findItem(R.id.map_terrain);
            menuItem.setChecked(true);
            break;

        case R.id.map_normal:
            menuItem = (MenuItem) menu.findItem(R.id.map_normal);
            menuItem.setChecked(true);
            break;

        case R.id.map_hybrid:
            menuItem = (MenuItem) menu.findItem(R.id.map_hybrid);
            menuItem.setChecked(true);
            break;
    }

    return true;
}

答案 1 :(得分:1)

MenuItem中的{p> onOptionsItemSelecteditem.isChecked()方法,只需使用它即可。你可以存储一个布尔字段(在我看来它不会是坏事)并且每当发生radiog组的变化时就改变它

然后,只需致电:

即可获得您的身份证明
if(item.isChecked()) {
     your_id_field = item.getItemId()
}

答案 2 :(得分:0)

创建一个变量eg:private int menu_selection = 0; 来存储所选菜单项的id。最初你可以定义0

onSaveInstanceState

使用onRestoreInstanceState@Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the id of radio button selected in the menu savedInstanceState.putInt("selection", menu_selection); super.onSaveInstanceState(savedInstanceState); } @Override public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance menu_selection = savedInstanceState.getInt("selection"); }

保存并恢复变量的值
onOptionsItemSelected

item.getItemId()中,将选定的@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection if (item.isChecked()) { item.setChecked(false); } else { item.setChecked(true); menu_selection = item.getItemId(); } switch (item.getItemId()) { case R.id.map_terrain: //some action here return true; case R.id.map_normal: //some action here return true; case R.id.map_hybrid: //some action here return true; default: return super.onOptionsItemSelected(item); } } 菜单项的ID指定给变量

onCreateOptionsMenu

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); if (menu_selection != 0) { MenuItem selected = (MenuItem) menu.findItem(menu_selection); selected.setChecked(true); } return true; } 中,找到用变量中的值标识的菜单项并进行检查

{{1}}