0

我检查有问题onOptionItemSelected()。我建立了一个菜单,onOptionItemSelected()有 2 个菜单组,第一组只有 1 个子菜单,第二组有 3 个子菜单。当我单击它时,我想更改子菜单上的检查。但是我试了很多次,还是不行。

如果我点击了一个子菜单,它的检查会改变,但其他检查不会改变。谁能帮我 ?

我将所有检查内容保存到共享首选项。

任何人都知道有关访问其他项目检查对象的任何信息onOptionItemSelected()吗?

谢谢你,对不起我的英语。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    String ringerString = mSharedPrefs.getString("ringer", SOR_STRING);
    boolean ringer = false;
    if (ringerString == SOR_STRING) {
        ringer = true;
    }
    String ayarString = mSharedPrefs.getString("ayar", "ikisi");
    boolean ayarIkisi = false, ayarMesaj = false, ayarArama = false;
    if (ayarString == "ikisi") {
        ayarIkisi = true;
    } else if (ayarString == "mesaj") {
        ayarMesaj = true;
    } else if (ayarString == "arama") {
        ayarArama = true;
    }
    SubMenu mMenu = menu.addSubMenu("Settings").setIcon(
            android.R.drawable.ic_secure);
    mMenu.add(1, 11, 0, SOR_STRING).setCheckable(true).setChecked(ringer);
    menu2 = menu.addSubMenu("Mesaj ve Arama Seçenekleri").setIcon(
            android.R.drawable.sym_action_chat);
    menu2.add(2, 12, 0, "Mesajlarda cevapla").setCheckable(true)
            .setChecked(ayarMesaj);
    menu2.add(2, 13, 0, "Aramalarda cevapla").setCheckable(true)
            .setChecked(ayarArama);
    menu2.add(2, 14, 0, "Her ikisinde de cevapla").setCheckable(true)
            .setChecked(ayarIkisi);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    RunningServices runningServices = new RunningServices();
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    String ringerString = mSharedPrefs.getString("ringer", SOR_STRING);
    boolean ringer = false;
    if (ringerString == SOR_STRING) {
        ringer = true;
    }

    switch (item.getItemId()) {
    case 11:
        if (ringer) {
            Log.v("menu sor", "giriş");
            if (!runningServices.isRingerRunnig(manager)) {
                try {
                    startService(new Intent(MainActivity.this,
                            RingerService.class));
                } catch (Exception e) {
                    // TODO: handle exception
                    Log.v("start service", "ringer service start hatası");
                }
                doShared("ringer", SORMA_STRING);
                item.setChecked(true);
                // setMenuTitle(mSharedPrefs.getString("ringer",
                // SOR_STRING),
                // item);
                Log.v("menu", item.getTitle().toString());
                progresislemi();
                return true;
            }
        } else {
            Log.v("menu sorma", "giriş");
            // if (runningServices.isRingerRunnig(manager)) {
            try {
                stopService(new Intent(MainActivity.this,
                        RingerService.class));
            } catch (Exception e) {
                // TODO: handle exception
                Log.v("stop service", "ringer service stop hatası");
            }
            doShared("ringer", SOR_STRING);
            item.setChecked(false);
            // setMenuTitle(mSharedPrefs.getString("ringer", SOR_STRING),
            // item);
            Log.v("menu", item.getTitle().toString());
            progresislemi();
            return true;
            // }
        }
        return true;
    case 12:
        doShared("ayar", "mesaj");
        Toast.makeText(getApplicationContext(),
                mSharedPrefs.getString("ayar", "mesaj"), Toast.LENGTH_LONG)
                .show();
        menuIslemi();
        item.setChecked(true);
        return true;
    case 13:
        doShared("ayar", "arama");
        Toast.makeText(getApplicationContext(),
                mSharedPrefs.getString("ayar", "mesaj"), Toast.LENGTH_LONG)
                .show();
        menuIslemi();
        item.setChecked(true);
        return true;
    case 14:
        doShared("ayar", "ikisi");
        Toast.makeText(getApplicationContext(),
                mSharedPrefs.getString("ayar", "mesaj"), Toast.LENGTH_LONG)
                .show();
        menuIslemi();
        item.setChecked(true);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
4

1 回答 1

0

我建议将您的菜单项设置为onPrepareOptionsMenu()而不是onCreateOptionsMenu(). 这将在每次访问时动态重建您的菜单,以便正确显示修改后的检查值。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {...}
于 2013-02-13T00:33:34.230 回答