What are the practical habits in Android?
This article is to share with you about the practical habits in Android, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. All classes define TAG for ease of debugging. Log is recorded in all methods (or at the beginning of critical steps).
Class TestService extends SuperClass {private static final String TAG = "text.TestService"; / / some code... Private void method1 () {Log.i (TAG, "method1"); / / some code... }}
2. In Activity, many controls have events such as click, and it seems messy to register each control. You can let Activity implement OnXXXXListener. In the onXXX () method, the process is unified.
Class MyActivity extends Activity implements OnClickListener {
/ / some code
Public void onCreate (Bundle savedInstanceState) {
InitComponent ()
RegisterListenr ()
}
Private void initComponent () {
MTextView = (TextView) findViewById (R.id.id1)
MTextView2 = (TextView) findViewById (R.id.id2)
/ / some code
}
Private void registerListener () {
MTextView.setOnClickListener (this)
MTextView.setOnClickListener (this)
}
Private boolean onClick (View v...) {
Switch (v.getId ()) {
Case R.id.id1:
Break
Case R.id.id2:
Break
Default:
Log....
}
}
}
3. Reuse layout layout files by include as far as possible. Easy to manage, save time and keep the code simple.
4. Use style to uniformly set the properties of the control. In order to avoid inconsistent style, but also save code.
5. No mixing is used before the official package is released. Otherwise, you will not be able to view the log after testing the bug. You can only see a.b.c.
These are the practical habits in Android, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.