One, the Android code specification
1. Learn to use String.xml files
In my opinion, when a text message appears more often than once, it must be used String.xml
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", then all the save buttons in an app are written like this. When one day to modify the requirements, the "save" text to change to "submit", then we can only go to a layout file changes,
It would be a waste of time and there might be a situation where the changes were missed.
Standard wording:
<button android:id= "@+id/editinfo_btn_save" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "@string/save" />
In the String.xml file:
<string name= "Save" > Save </string>
This kind of writing, need to change later, only need to modify a line of code in the String.xml file to implement the whole app's text content modification.
2, learn to use Color.xml, the use of dimens.xml files
Consistent with the use of string.xml, students should all understand, do not because of a momentary lazy, resulting in later iterations of time-consuming effort.
3, team work together to determine a set of standard activity of the OnCreate () method of code execution process
In fact, when I first came into contact with 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)); }
Whatever, all the operating code is written in the OnCreate () method, including the find control. Set up listening events, load data sources, and so on.
Can see now 2 control a data source, the code is so much, if an interface has more than 10 controls, then the OnCreate () method of the code in the number of multiples.
So all the activity has to set a uniform specification.
As we all know, there are basic operations in an activity:
①, initializing variables
②, initializing controls
③, setting Listener events
④, load network data and display
Then you can put the above code in the various categories of these methods
For example, a canonical 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 (); }//Initialize variables, such as the intent data from the previous activity. private void Initvariables () {}//Load data source private VO ID LoadData () {}//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 ()) {caseR.id.btn_scan://scan QR code startactivity (new Intent (This,scanactivity.class)); Break Case R.id.btn_create://Generate two-dimensional code startactivity (new Intent (This,createactivity.class)); Break } }}
Can see, onCreate () in just a few methods, we need to find the problem in the appropriate way to find can, both convenient and clear.
In fact, this kind of operation we can write a baseactivity as its abstract method, and then let the activity inherit this Baseactivity base class rewrite method, involving the schema, this later.
4, team work together to determine a control of the Click event Onclicklistener ()
Android gives us 5 ways to set the onclick for the control, and personally think that the most used item in the project is
①, parameter This then activity implements View.onclicklistener interface rewrite OnClick () method
Btn_create.setonclicklistener (this);
②, the direct parameter of the new Onclicklistener ()
Btn_create.setonclicklistener (New View.onclicklistener () { @Override public void OnClick (View v) { } });
Three other methods the individual feels as far as possible not to use. The first of these two methods is better, because we can distinguish the click events of different controls with the Switch--case method, and the code is more concise and simple.
Of course, the second method is also possible, but it is best to remember that the same project has only one way to facilitate later maintenance.
Set Click event @Override public void OnClick (View v) { switch (V.getid ()) {case R.id.scan: //scan QR code startactivity (New Intent (This,scanactivity.class)); break; Case R.id.create: //Generate two-dimensional code startactivity (New Intent (This,createactivity.class)); break; } }
--------------------------------------------------------------------------------------------------------------- -----------
Second, the Android naming specification
Naming specification: Hump method, Underline segmentation method.
1. Java class files
①, activity: With activity as the suffix, this believes everyone as to you to help you do well.
②, Adapter: with Adapter as suffix
③, Viewholder: with Viewholder as suffix
④, entity class entity: with entity as suffix
As below, I am a function Module sub-package, big God do not spray, personal preferences:
2. xml file
①, Layout.xml
The activity's layout file begins with Activity_, as provided by the.
Layout file for list items The ListView starts with Item_list_.
②, naming of controls
Abbreviation, this looks personal,
My experience, for example:
Layoutview----LV
TextView----TV
Button----BTN
ImageView----img
Remember, do not use pinyin naming, even if the English dish of bloggers I developed all open Youdao dictionary.
Finally, the code must write the comments, you name if the English is not immediately able to understand, please make sure to write the comments.
Encoding Specifications for Android