Objective:
Currently working in charge of the development of two medical app projects, and using Leancloud for cloud development, complete one-on-one.
Now the large framework has been completed, the development of the details module
Take the time to sum up the development specification of Android Project: 1, Code Specification 2, naming code
Note: Personal experience, for reference
I. Android Coding specification
1. Learn to use String.xml files
In my opinion, it is necessary to use String.xml when a text message appears more often than once.
For example, a Save button, not the canonical wording:
<button
android:id= "@+id/editinfo_btn_save"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:text= "save"
/>
Here the text content is set to "save", so all the save buttons in an app are written like this. When the need to change one day, the request to "save" text to "submit", then we can only go to a layout file to modify,
It is not a waste of time and there may be a missed change.
Standard writing:
<button
android:id= "@+id/editinfo_btn_save"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:text= "@string/save"
/>
And in the String.xml file:
<string name="save">保存</string>
This kind of writing, need to modify later, only need to modify a line of code in the String.xml file to realize the whole app of the text content modification.
2, learn to use Color.xml, Dimens.xml file use
Consistent with the use of string.xml, students should be able to understand, do not because of laziness, resulting in late iterations of time-consuming and laborious.
3, the team to identify a set of standard activity in the OnCreate () method of code execution flow
In fact, when I first contacted Android, my nonstandard code was this:
Private Button scan; Scan button
Private button create; Create button
private arraylist<object> datas//Data source
@Override
protected void OnCreate (Bundle Savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Create = (Button) Findviewbyid (r.id.create);
Scan = (Button) Findviewbyid (R.id.scan);
Scan.setonclicklistener (this);
Create.setonclicklistener (this);
Datas = new arraylist<> ();
Datas.add (New Integer (1));
Datas.add (New Integer (2));
Datas.add (New Integer (3));
Datas.add (New Integer (4));
No matter what, all the operation code is written in the OnCreate () method, including the find control. Set up listener events, load data sources, and so on.
You can see now 2 controls a data source, the code is so much, if an interface has more than 10 controls, then the OnCreate () method in the number of code is multiplied.
So all activity has to set a uniform standard.
As we all know, there are basic operations in an activity:
①, initializing variables
②, initializing controls
③, setting up listening events
④, load network data and display
Then you can put the above code in the following categories in these methods
For example, a section of spec code:
public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private Button btn_scan; Scan button Private button btn_create; Create button private arraylist<object> datas;
Data source @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initvariables ();
Initview ();
Initevent ();
LoadData (); }//initialization variables, such as the intent data from the previous activity some tag variables in this activity, such as private void Initvariables () {}//load data source private void Loaddat
A () {}//Register listener event private void Initevent () {Btn_scan.setonclicklistener (this);
Btn_create.setonclicklistener (this);
}//Initialize control private void Initview () {btn_create = (Button) Findviewbyid (r.id.create);
Btn_scan = (Button) Findviewbyid (R.id.scan); //Set Click event @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_scan://Scan two-dimensional code startactivity
(New Intent (This,scanactivity.class));
Break Case R.id.btn_create://Generate two-dimensional code Startactivity (New Intent (This,createactivity.class));
Break
}
}
}
It can be seen that onCreate () in a few ways, we need to find a problem in the appropriate way to find it, both convenient and clear.
In fact, we can write a baseactivity as its abstract method, and then let the activity inherit the Baseactivity base class rewrite method, involving the architecture, this later.
4, team work together to determine a control of the Click event Onclicklistener ()
Android gives us 5 ways to set the onclick control for controls, and it's the one thing that most people think about the project
①, parameters This then the activity implements the View.onclicklistener interface overrides the OnClick () method
btn_create.setOnClickListener(this);
②, the direct parameter new Onclicklistener ()
Btn_create.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
}
});
The other three methods are personally felt as if they were not used. The first of these two methods is better, because we can distinguish the click events of different controls by the Switch--case method, and the code is more clear and simple.
Of course, the second method is also possible, but it is best to remember that in the same project there is only one way to facilitate later maintenance.
Set Click event
@Override public
void OnClick (View v) {
switch (V.getid ()) {case
R.id.scan:
//Scan two-dimensional code
startactivity (New Intent (This,scanactivity.class));
break;
Case R.id.create:
//Generate two-dimensional code
startactivity (New Intent (This,createactivity.class));
break;
}
}
5, as far as possible with the global variable static transfer value operation
Static characteristics We should all know that he will always occupy a part of the memory, although few, but a project in the hundreds of thousands of items is very bad.
Suggested that the transfer between the page value or with intent, to achieve no good solution of the use of static, PS, I have previously the company's projects are very large use of static
Note: Some students may be on the activity and fragment of the exchange value is not General Assembly, here recommend a solution: Talk about Eventbus, for me is a project must, but also to reasonable use
6, the activity as far as possible not to use the internal class
Here to Recyclerview examples, a very good control, with it no longer need to ListView, talking about Recyclerview (perfect alternative Listview,gridview)
A recyclerview is matched by a adapter and a viewholder.
Nonstandard practice: Part of the classmate diagram (of course, in an activity is really convenient data transmission and item Click event operation), it is not advisable to write them all in an activity, because greatly increased the amount of code for a single activity, is very inconvenient for maintenance.
Normative approach: Viewholder A class, adapter a class, clear division of labor, to avoid a class of excessive amount of code.
Note: Listview,viewpager use ditto
As for class classification, some students like a functional module placed under a package, such as a function point of activity, Adapter, Viewholder are placed under a package
Some students like a kind of class placed under a package, such as all the activity placed under the Activitys package, all the adapter placed under the adapters package.
This is the something, the future study of the structure of the time to discuss the details
7. Use ArrayList instead of HashMap
It is said that the ArrayList use of memory is said to be lower than the HashMap, because the Android phone is uneven, so the development process of memory is very important, can save the province.
PS: The basic use of ArrayList in my project, unless it is a ArrayList in place of the data structure
8. The third party of the Unified Team standard
Now convenient and easy to use the third party too much, the picture frame several excellent, pushed several excellent, instant messaging several excellent.
Note that using too many third parties can cause the program to be too large, and the application has a maximum number of methods to avoid the implementation of a third party, the team members are using a different third party.
9. Unified Code Format
The classic is for loops, one for the left parenthesis at the end and one for the other. Unified, the interface looks comfortable, personal advice left parenthesis put in the last way, don't ask me why, university teacher recommended, Reason: forget.
for (int i = 0; i < i++) {} for
(int i = 0; i < i++)
{
}
10, the different function code to have a line of space separate
In conjunction with the note, tell the maintenance of the students, which piece of code is what the operation
For the sake of the code is clear also in order to maintain the classmate to have two hairs less long.
11, if you are the Android Studio development
Please use CTRL + a frequently--"Ctrl +alt + I
--------------------------------------------------------------------------------------------------------------- -----------
Second, the Android naming code
Naming code: Hump method, Underline segmentation method.
1. Java class file
①, activity: With activity as a suffix, this believe that everyone as to you have done for you.
②, Adapter: with Adapter as suffix
③, Viewholder: with Viewholder as suffix
④, Entity class entity: Entity as suffix
as follows, I am a functional module subcontracting, the great God do not spray, personal preferences:
2. xml file
①, Layout.xml
The activity's layout file begins with the Activity_, as provided.
The layout file for the list item ListView begins with Item_list_.
②, the name of the control
Abbreviation, this looks personal,
My experience, for example:
Layoutview----LV
TextView----TV
Button----BTN
ImageView----img
Remember, do not use the pinyin name, even if the English such dish of bloggers I developed all open Youdao dictionary.
Finally, the code must write a note, you named if English is not immediately able to understand, please be sure to write a note.