Подтвердить что ты не робот

Как раздуть меню настроек Android и установить для параметра значение Enabled = false?

Определение моего XML-меню устанавливает для элемента с включенным состоянием R.id.menu_refresh значение false. Когда приложение запускает пункт меню, он неактивен и отключен. Почему этот код в приложении не включает элемент?

public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    MenuItem refresh = menu.getItem(R.id.menu_refresh);
    refresh.setEnabled(true);
    return true;
}

Что мне не хватает?

4b9b3361

Ответ 1

Попробуйте menu.findItem() вместо getItem(). getItem() принимает индекс от [0, размер), а findItem() принимает идентификатор.

Ответ 2

это то, что я делаю в своей деятельности для обработки меню...

//Android Activity Lifecycle Method
// This is only called once, the first time the options menu is displayed.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}


//Android Activity Lifecycle Method
// Called when a panel menu is opened by the user.
@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
    MenuItem mnuLogOut = menu.findItem(R.id.main_menu_log_out_id);
    MenuItem mnuLogIn = menu.findItem(R.id.main_menu_log_in_id);
    MenuItem mnuOptions = menu.findItem(R.id.main_menu_options_id);
    MenuItem mnuProfile = menu.findItem(R.id.main_menu_profile_id);


    //set the menu options depending on login status
    if (mBoolLoggedIn == true)
    {
        //show the log out option
        mnuLogOut.setVisible(true);
        mnuLogIn.setVisible(false);

        //show the options selection
        mnuOptions.setVisible(true);

        //show the edit profile selection
        mnuProfile.setVisible(true);
    }
    else
    {
        //show the log in option
        mnuLogOut.setVisible(false);
        mnuLogIn.setVisible(true);

        //hide the options selection
        mnuOptions.setVisible(false);

        //hide the edit profile selection
        mnuProfile.setVisible(false);
    }

    return true;
}


//Android Activity Lifecycle Method
// called whenever an item in your options menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle item selection
    switch (item.getItemId())
    {

    case R.id.main_menu_log_in_id:
    {
        ShowLoginUI();
        return true;
    }

    case R.id.main_menu_log_out_id:
    {
        ShowGoodbyeUI();
        return true;
    }

    case R.id.main_menu_options_id:
    {
        ShowOptionsUI();
        return true;
    }

    case R.id.main_menu_profile_id:
    {
        ShowProfileUI();
        return true;
    }

    default:
        return super.onOptionsItemSelected(item);
    }
}

Мне нравится этот подход, потому что он делает код приятным и модульным