Domanda

I'm following the guide for Copy and Paste on the Android Developer's page. However, there's a section I don't quite get, which is the section on pasting with the plain text:

// Gets the ID of the "paste" menu item
MenuItem mPasteItem = menu.findItem(R.id.menu_paste);

// If the clipboard doesn't contain data, disable the paste menu item.
// If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {

mPasteItem.setEnabled(false);

} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {

    // This disables the paste menu item, since the clipboard has data but it is not plain text
    mPasteItem.setEnabled(false);
} else {

    // This enables the paste menu item, since the clipboard contains plain text.
    mPasteItem.setEnabled(true);
}
}

I can understand most of it, but what caught me off guard was the use of a member variable. I know that these guides aren't 1:1 code, but I was following just find and saw no mention of a variable named "menu." So, I'm asking to those who probably know Android more than I do, what is the purpose of this variable? I know that I went back and edited the menu.xml file so that I had a "Copy" and "Paste" item (which this "tutorial" seems to use), but now I don't know how to instantiate/initialize this menu, nor do I really know its purpose. Can someone explain this to me?

Thanks.

È stato utile?

Soluzione

Silly Android developers, using undocumented variables...

That's a reference to some layout containing the Menu in which the paste button (a MenuItem) can be found. This item has ID menu_paste, which you may or may not already know.

Realistically, this menu variable can be any Menu layout that contains your menu item. You can find out how to create Menus here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top