Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the programming specification of Android

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the programming specification of Android". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the programming specification of Android" can help you solve the problem.

Naming rules

1)。 Class name, API name:

Start with uppercase. If the class name of a class consists of multiple words, the first letters of all words must be capitalized. Try to write the full name of the word, not abbreviated, unless established names, such as widely used proper nouns such as URL,RTMP,RTSP, can be capitalized or capitalized.

For example, HttpRequest,CourseActivity

2)。 Local variables, class member variables, class member functions, function parameters:

Other words that begin with lowercase letters are capitalized. It is not recommended to use underscores to separate words for variable names. Hump naming is recommended, which is used in Android's system classes.

For example, toString () onCreateView (Bundle savedInstanceState)

3)。 Static constants: all capitals, words are separated by underscores, constant words are all capitalized, so words are separated by underscores.

For example, WHAT_EMPTY_CONTENT

4)。 Control variable naming, control ID naming:

Recommendation: the naming of the id of the control in the xml layout file is the same as the naming of the control object in the code file of * .java.

Class MyActivity extends Activity {TextView txtUserName; … Protected void onCreate (Bundle savedInstanceState) {txtUserName = (TextView) findViewById (R.id.txtUserName);}}

5)。 Specification for naming common controls and class objects (the red part is the recommended prefix or suffix):

Class name

Variable name

Class name

Variable name

TextView

TxtDescription

ProgressBar

ProgressDescription

Button

BtnDescription

SeekBar

SeekBarDescription

ImageButton

ImgBtnDescription

VideoView

VvDescription

ImageView

ImgDescription

Spinner

SpinDescription

RadioButton

RbDescription

WebView

WebViewDescription

EditText

EditDescription

ListView

ListViewDescription

ScrollView

ScrollDescription

GridView

GridDescription

Handler

DescriptionHandler

RatingBar

RatingBarDescription

PullToRefreshListView

PullRefreshViewDescription

Adapter

DescriptionAdapter

Fragment

DescriptionFragment

Activity

DescriptionActivity

List

DescriptionList

Map

MapDescription

SlidingMenu

SlidMenuDescription

ViewPager

ViewPagerDescription

CheckBox

ChBoxDescription

View

ViewDescription

RadioGroup

RgDescription

ExpandableListView

ExpDescription

FrameLayout

FrameLayDescription

SharedPreferences

SpDescription

LinearLayout

LineLayDescription

RelativeLayout

RelativeLayDescription

StartActivityForResult (requestCode)

REQUEST_CODE_DESCRIPTION

Msg.what

WHAT_DESCRIPTION

6)。 Resource naming:

Naming of layout resource files (all lowercase, underscore delimited):

Resource file for activity: activity_description1_description2.xml

Resource file for fragment: fragment_description1_description2.xml

Resource file for listview list items: list_item_description1_description2.xml

Reusable (include) component resource file: control_description1_description2.xml

Drawable resource: controlName_description1_description2_selector.xml

ControlName indicates what type of control the resource is to be used for, for example, if it is the picture switch of a button

This is how button_bg_sendmessage_selector.xml should be defined.

Selector indicates the form of the resource, such as shape, etc.

Name of picture resource: ditto

Naming of color values: color_description is prefixed with color, all lowercase, and underscores are separated. Description can be either a functional description of the color value, an English description of the color value, or a specific color value, for example:

# ffffff # cccccc # dddddd

Because grey may have many grades, sometimes different levels of gray are needed, and there are not so many English names to distinguish, so color values can be used directly in names.

# 4c4c4c defines description according to function, indicating that the color is used for buttons to be pressed

Note: meaningless naming, such as textview1,textview2, is not allowed.

About literal constants

Direct hard-coded literal constants are not allowed in the code, and if it is the text displayed on the control, it must be placed in the strings.xml resource file. If a constant string is used in the code, it must be defined as a constant value of type public static final String, and the constant value of that definition must be used in the code. The advantage of this is that you need to change the constant value later, only in one place. If it is hard-coded in the code, you have to modify all the places where it is used, and the copy is error-prone. When passing parameters between Activity, the key value of intent.putExtra should also be named by convention and uniformly defined as static constant, which cannot be hard-coded directly in the code, otherwise it is troublesome to modify it. An Activity needs to accept parameters when it is started, so the key definition of these parameters should be placed in that Activity.

JSON parsing

The interface that calls the server in Android generally returns json data. When parsing json, whether using the original manual parsing method or using javabean parsing method, the parsed results must be null-judged when used. App is not allowed to crash when parsing json because of a problem with the json on the server side.

Class member initialization

The member variables of all classes must be assigned initial values, not only defined, but not assigned.

Int type constant

When the function returns, if the returned data of type int is not real practical data values (such as height, width, size, etc.), it only indicates the successful, failed, abnormal state values of the function, and these values are limited. These values must be described as static constants, or enumerations, such as:

Int GetJsonString ()

This function returns-1 to get parsed json data exception, 0 to succeed, 1 to network connection exception, and 2 to empty data in json content. Then do not use these literals directly in the code inside the function, these literals are meaningless to the programmer, and the code is not readable, so it is recommended to make the following pattern:

Public static final int RESULT_PARSE_JSON_EXCEPTION =-1; public static final int RESULT_SUCCESS = 0; public static final int RESULT_NETWORK_EXCEPTION = 1; public static final int RESULT_NO_DATA = 2

The advantage of using these symbolic constant values instead of literal values is that symbolic constant values are made up of uppercase English words, which is meaningful and can help programmers better understand the meaning of function return values. and the specific assignment corresponding to the symbolic constant value is easy to modify at a later stage.

Activity accepts parameters and modularization

If an Activity may be opened in multiple places, or a Fragment may be used in more than one place. So when designing the Activity and Fragment, we must consider the low coupling, provide a unified parameter interface to the outside world.

The process of dynamic Activity is encapsulated in the static member method of the Activity class, similar to the following: class MyActivity extends Activity {. Public static void startActivity (Context context,Params param) {Intent intent = new Intent (context, MyActivity.class); intent.putExtra ("param", param); startActivity (intent);} public static void startActivityForResult (Context context,Params param) {Intent intent = new Intent (context, MyActivity.class); intent.putExtra ("param", param); startActivityForResult (intent,REQUEST_CODE);}}

Parameter passing * is encapsulated in a Model entity class to avoid using Map to pass parameters. It is recommended that the entity class be implemented as the static serializable inner class of the corresponding Activity.

AndroidStudio project catalogue organization

The package structure of the project in AndroidStudio should be organized according to the functions of each part of the project.

Encapsulation of Handler

Almost every Activity defines a Handler inner class, but many Handler in Activity use duplicate message types, in which there is redundant code, so the message part of the Handler class used by these Activity should be extracted into a common Handler class. Inheritance is then used in each Activity to provide a Handler class implementation of the Activity-specific Handler message type.

In addition, Handler should use the member function of the Handler class to send messages, not handler.obtainMessage (xxx). SendToTarget () directly; this original way of sending messages is not conducive to reducing coupling, and this detail should be hidden in the Handler. The message type of Handler should be defined as a static constant in the Handler class, which should not be public and invisible to the outside. That is, the details of sending messages using handler objects should not be exposed to the outside world.

Data updates for List

Encapsulate the data update of ListView and update the data in handlerMessage to avoid java.lang.IllegalStateException problems

Passing parameters between Activity and Fragment

The data transfer between Activity and Fragment adopts interface, which reduces coupling and facilitates the reuse of Fragment:

Network request data modularization

Generally, in Activity, we get the data through the interface of the network request server. This process is usually done in a thread. After getting the data, we send a message through the handler in Activity to inform Activity to update the data. The thread class responsible for getting data is generally implemented as an internal class of Activity, which can directly access the member variables of Activity, such as handler, data list objects, etc. However, this is not conducive to the reuse of the data acquisition thread. If another Activity needs to get the same data, then this function cannot be reused, so the thread class responsible for data requests should not be too closely related to the specific Handler and Activity. It should be defined as a static class, and handler should be passed in as a parameter, rather than directly accessing member variables of the external class.

Encapsulate Log function

The Log function should be encapsulated to automatically change the class name of the current class into the TAG parameter of the log output, the released app*** can circulate the log file to the system storage, and the log file should be reused repeatedly. The following is just an example of imperfection:

Public class MyLog {public static final String TAG = "myapp"; public static void v (Object oMagneString message) {Log.v (TAG+o.getClass (). GetSimpleName (), message);}}

Use

MyLog.v (this, "hello log")

Print the result

V/myapp MainActivity: hello log

Version control

Using automated version management, the version number is automatically generated to keep the version of the application consistent with that on the version library. You can use hg to replace the build.gradle file in the app directory under the project directory. If the version number is also set in manifest, AndroidStudio is still subject to build.gradle. You should not manually change the version number in the project settings of AndroidStudio every time you release it.

Add a global exception catch to the program

Global exception trapping should be added for app. There will always be some exceptions that we do not catch in app. Once users encounter such exceptions during use, the program will crash. We should detect such uncaptured exception information and obtain exception information by writing file logs or sending e-mail when the program crashes, so as to solve bug.

This is the end of the content about "what is the programming specification of Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report