Android代码规范

匿名 (未验证) 提交于 2019-12-03 00:34:01

代码规范目的:易读可维护

1:命名:
大驼峰(类(组件,接口,工具类))
小驼峰(方法,变量)
小写(包)
大写(常量,枚举)

2:尽量在每一个类中加入TAG,调试用途,若用框架,就不用,比如Logger

private static final String TAG = "DieselPayActivity"; Log.i(TAG,title);  FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()                 .showThreadInfo(false)                 .methodCount(2)                 .methodOffset(0)                 .tag("AILE_LOG")                 .build(); Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy)); Logger.d(title); Logger.json(result);

3:使用Intent传递数据,声明Key时,定义常量,是常量的肯定定义常量

public static final String MERCHANT_NAME = "上海车高电子商务有限公司"; public static final String MERCHANT_NO = "1136410005"; public static final String TERMINAL_N0 = "121715"; public static final String OPERATOR = "01";  public static final String FUELT_TYPE="fuelt_type" Intent intent= new Intent(context, SettingActivity.class);  intent.putExtra(FUELT_TYPE,"desel"); context.startActivity(starter);

4:增加类注释,使用Android Studio的 File And Code Template,我只是添加一行,
所有的常量加上注释,且功能相同的排放在一起,不同的进行换行
代码中不出现中文,最多注释中可以出现中文

  //服务器端请求URL public class UrlContainer {     public static final String HOST_URL = "http://192.168.2.14:8080/";     //签到     public static final String SIGN_IN = HOST_URL + "a/Signin.json";     //余额     public static final String ACCOUNT_BALANCE = HOST_URL + "a/AccountBalance.json";     //消费     public static final String ACCOUNT_CONSUME = HOST_URL + "a/AccountConsume.json";     //撤销     public static final String ACCOUNT_REVOKE = HOST_URL + "a/AccountReverse.json";  }

5:Activity中变量采用m开头+类名。实例成员变量前缀m(表示member),静态字段命名以s开头(表示static)

接口命名需要简单明了,长度不宜过长,建议在名称前面追加“I”,如IMagCardReader

除for循环变量外,一律不得使用i、j、k等单字符作为变量名

控件变量添加组件前缀,顺序在所有者前缀之后 ,如etCarNum

6:控件Id命名,控件缩写 模块(module) 功能名(function)

android:id="@+id/car_title_tv"

Color资源命名,组件名+作用名

 <color name="btn_login_checked">#a76605</color>

String资源命名,具体功能

<string name="app_name">myApp</string>

Drawable资源命名,ic_具体模块_功能,如ic_launcher_background.xml

7:一般不使用System.out输出,而使用Log
System.out,一堆东西,eclipse好用
Log:有级别区分,Log.v()、Log.d()、Log.i()、Log.w()、Log.e();容易加过滤器,可以看到自己想看到的Log信息

文章来源: Android代码规范
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!