"Android" UI component variable organization

Source: Internet
Author: User

DescriptionThis is not a technical article, but because the recent project to a few new Android, so I figured in the code style for a unified, so there are the following and a few articles later.Java file internal UI component variables OrganizationThe UI component uses the lowercase letter "V" as the prefix.

V mainly refers to "View", the Android source code, the variable prefix is mainly "M", but the personal feel for the UI variables, or "V" is relatively clear

Intent-oriented, minimized abstraction

The "declaring type" of a UI variable should be a minimal abstract type that implements the intent, and the variable name needs to be able to represent the functionality of a component.

For example, the interaction requirement has a ImageButton, and the action after the click Triggers a search request. How should the variables be organized?

First step: declaring type

There are probably several ways to declare this variable:

1. private ImageButton varName;2. private ImageView varName;3. private View varName;

The problem with these statements is that if this button simply triggers a click event,
Then it shows that our intentions only need to use the view level, because View.setonclicklistener (Onclicklistener) is the only feature we care about, so we just need to limit the type to the view level.

In addition, the UI is a relatively high frequency of changes in the section, the more abstract it is easier to modify.
In other words, "interface-oriented programming", where view acts as an interface, so that we do not change the definition of ImageButton in the future, either Imageview,button or TextView.

Second Step: variable name

There are several possible names for variables:

1.

    ImageButton vImageButton1

This type of naming should only appear in the tutorial code, which should never be seen in the project code.

2.

    ImageButton vQueryImageButton(或者vQueryBtn)

This name is acceptable, but one problem is that the variable name involves a specific component type, as explained in the first step above, and if you do not use the "ImageButton" specific method, you do not need to materialize a variable too much.

3.

    ImageButton vSearch

This naming is also acceptable, and more applicable.

4.

    ImageButton vSearchAction

Compared to the above vsearch, individuals are more inclined to this kind of naming.
While all are purely intent-related, one problem is that it may take several components to complete an intent, such as a search that typically has an input box that can be named Vsearchcontent, so that the suffix "Action" and "Content" can be used Separating two parts from each other

XML internal component ID naming

The same is true of the ID name in the XML file, which needs to be provided to the Java file with as much intent wire as possible.
For example, there is no need to include the full "relativelayout" field in the Relativelayout naming because the actual project needs to invoke the Relativelayout specific method is basically not the case. So you can use a more generic name like container.

For components that are used for positioning or anchor points only in layout files, you can name the IDs more clearly, such as using names such as Local,anchor to tell Java code that they should not be arbitrarily fetched, because these IDs are not stable fields.

Android Share Q Group: 315658668

"Android" UI component variable organization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.