List of reserved words in Android

2019-04-24 01:09发布

问题:

I'm currently in the interface design process of developing another Android app and once again I seem to be trying to use reserved words for the resources (be it drawables and layouts). To my knowledge there are a set of rules you need to know:

  • No uppercase is allowed.
  • No symbols apart from underscore.
  • No numbers

Appart from those (please correct me if I'm wrong) I think you can't use any of the reserver words from JAVA which after a little googling appear to be the following:

So my question would be if there is somewhere in the docs that I've failed to locate, that explains in detail what we can and can not use for resource names. This is right after reading the page about resources so its possible that I'm simply worthless in reading.

Source for the reserved words

回答1:

To my knowledge there are a set of rules you need to know:

No uppercase is allowed.

AFAIK, that is not a rule. A convention is to use all lowercase, but mixed case has worked.

NOTE: In layouts you can ONLY use lowercase letters (a-z), numbers (0-9) and underscore (_).

No symbols apart from underscore

Correct. More accurately, the name has to be a valid Java data member name, which limits you to letters, numbers, and underscores, and it cannot start with a number.

No numbers

That is not a rule, though your name cannot start with a number, as noted above.

Appart from those (please correct me if I'm wrong) I think you can't use any of the reserver words from JAVA which after a little googling appear to be the following:

That is because reserved words are not valid Java data member names.

So my question would be if there is somewhere in the docs that I've failed to locate, that explains in detail what we can and can not use for resource names

Apparently not.



回答2:

Well my answer would be a mix of some pages where you can find what you need.

1.- First i would recommend you to read the Conventions that Oracle recommends for java

NOTE: especially the section of "Naming Conventions" (this is something that most of the other answers have), after that i would suggest you to read the "Java Languages Keywords" cause you can not use any of those words, BUT remember that JAVA is CASE-SENSITIVE, so if you write "Abstract" instead of "abstract" then is OK, but of course that may confuse someone later one (maybe yourself).

2.- Last but not least you can read the "Code Style Guidelines", this are the conventions that contributors to the android source code need to apply to their code to be accepted.

If you follow this rules, your code not only will be valid (Of course this is important), is going to be more readable for you and others, and if another person needs to make some modification later on, that would be a easier task than if you just start typing random names like "x1, x2, X1, _x1, etc..."

OTHER USEFUL ARTICLE:

If you are starting your app, then this article is going to be very useful for you, it explains why the use of setters and getters in a exaggerated way is a very bad practice, they need to be ONLY if is needed not just for setting and getting every variable in your object.



回答3:

If you use identifiers that are valid Java variable names (this means consist only of a-z, A-Z, 0-9 and the underscore characters) you will not have any problems. The actual namespace is probably larger, but this works for me.

documentation



回答4:

I'll just chip in and say this:

You can't use keywords but managing android resources isn't quite easy either... for instance, you cannot have different folders for drawables, they need to go to drawable-xxxx folder...

So, try to come up with sensible prefixes for your drawables and selectors.

Android accepts all valid Java variable names so I don't really see where this question comes from.