开发者

MenuItem#getTag()

开发者 https://www.devze.com 2023-03-05 08:55 出处:网络
On subclasses of View there is a开发者_如何学Go getTag() method, which returns the android:tag attribute\'s value from .xml.

On subclasses of View there is a开发者_如何学Go getTag() method, which returns the android:tag attribute's value from .xml. I would like the same for a MenuItem... is it okay to just cast it to a View? Because item elements also allow a tag attribute in .xml...

Update: My goal with this is setting a tag in .xml, i.e. "notranslate", and querying it at runtime (we localize by hand at runtime, don't ask...)


It is always alright to cast, however, casting any Interface cannot be checked at compile time, only runtime. This is normally the reason many do not recommend casting an Interface that you have no control over. Having the proper error checking code is the best way to insure that such a cast does not break your code.

For the casting, it doesn't really matter whether the MenuItem is an Interface or a View, but the object it references must be one of View's subclasses, if not a View itself. If you are going to cast it, try the cast and catch a ClassCastException just in case as this is the error that will be thrown in runtime.

Another option is that since the MenuItem is simply an interface, you can easily just create a View subclass that utilizes MenuItem allowing you to do the cast. If you are doing a custom ContextMenu as many launchers do, then chances are your answer is nearly complete.

Hope this helps, FuzzicalLogic


MenuItem is an interface. Any class can implement this interface and so it will not always be safe to cast the MenuItem to a View. You can use the "instanceOf" operator to test to see if the object that implements the MenuItem interface is indeed a View or not.

I understand that you want to define a flag in the XML definition of the menu and then at run time interrogate that flag to make a programmatic decision.

The Menu Resource Documentation records what attributes can be set in the XML. You can consider using (abusing) one of those settings such as the "android:alphabeticShortcut" to encode the flag and use the MenuItem::getAlphabeticShortcut() method to get the value. This does not require casting - it just uses the existing fields in the MenuItem XML construct/class for your own purposes.

Perhaps a less hacky way to do this is to keep a simple table in a separate assets file that lists the menu item identifiers and the special behavior associated with that identifier such as to translate or not to translate.

Alternatively create a simple class that has a table with this configuration information hard coded using the logical "@[+][package:]id/resource_name" resource identifier as the keys to the table. While this doesn't keep it all in one place (in the XML) it does it in a manner that is not encoding information in unused attributes, or relying on the ids not changing. The "table" could be implemented as a static method with an embedded switch statement allowing code such as "if (TranslationTable.shouldTranslate(menuItem.getItemId())) { do translation }"


I had a similar problem in that I wanted to associate some arbitrary data with each menu item so that I could handle menu items in a generic way without having to use hardcoded checks for individual item ids in code.

What I did was for a particular menu item (e.g. @+id/foo) There was an a TypedArray that was defined using the same name as the menu item ID. You could do this with other types of resources as well.

So to do the association, you get the resouce entry name (foo in my example) and then use that to look up the id of the other resource of a different type (@array/foo in my example).

In my handler for menu I had code like this:

Resources resources = getResources();
String name = resources.getResourceEntryName(item.getItemId());
int id = resources.getIdentifier(name, "array", "com.example");

if(id != 0)
{
    TypedArray data = resources.obtainTypedArray(id);

    // Use the typed array to get associated data
}

EDIT:

Actually it is even easier than that. There is nothing special about the ids on menu items other than you don't want multiple menu items with the same id. The id does not have to be of the form @+id/foo. It can actually also refer to other resources. So in my example above, instead of having the menu have an id of @+id/foo and using the resource manager to use that to find @array/foo, I changed to actually have the menu item have the id of @array/foo.

Now in my onOptionsItemSelected I have this:

Resources resources = getResources();

if("array".equals(resources.getResourceTypeName(item.getItemId())))
{
    TypedArray data = resources.obtainTypedArray(item.getItemId());

    // Use the typed array
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号