سؤال

Im trying to do something like this:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    //multiListener = false;
    menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.multiselect:
        if(multiselect == 0) { multiselect = 1;
        multiListener = true;
        Log.d("DH", "index="+multiListener);
        }
        else if(multiselect == 1) { multiselect = 0; multiListener = false;
        Log.d("DH", "index="+multiListener);
        }
        fillData();
        return true;
    case START_DELETE:
        Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
        return true;
    default: 
        return super.onOptionsItemSelected(item);

    }
}

Basically, if multiListener = true; make "Delete selected.." pressable otherwise gray it out...

This simple , thing.. doesn't want to work out with me,

for somehow... the button is always greyed out, although Log says that, it changes to true...

Anyone, know something?

هل كانت مفيدة؟

المحلول

You should call setEnabled() again to change item's state. Its state doesn't bind to a variable. This method should be called in onPrepareOptionsMenu().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top