安卓编码规范

一、 

1、 包命名全部采用小写,不用下划线区分单词

2、 主包名采用[公司性质].[公司名称].[项目名称]的命名方式
例如:翡翠做的手机助手包名edu.feicui.assistant

3、 通用功能子包名采用[主包名].[通用名称]的命名方式
常见通用功能如下表:

含义

[主包名].main

基础Activity及入口Activity所在的包

[主包名].util

通用工具类所在的包

[主包名].view

自定义View所在的包

[主包名].db

数据库操作相关所在的包

[主包名].pref

首选项

[主包名].provider

内容提供者所在的包

[主包名].receiver

广播接收器所在的包

[主包名].service

后台服务所在的包

一般功能子包名采用[主包名].[模块名称].[子模块名称]的命名方式
例如:
手机助手的闹钟模块包名 edu.feicui.assistant.alarm

4、 只需导入用到的类,不得用*导入包下所有类

5、 导入类时,系统类在上方,自定义类在下方

二、 代码

1、 代码主要采用大/小驼峰命名法,即除首字母外,每个单词首字母大写,整体首字母大小根据其它规范决定

2、 类名、接口名、枚举名等首字母大写,若由多个单词组成,则其后每个单词首字母大写
例如:

class ConfigManager{}

3、 接口一般使用able、ible、er等作为后缀
例如:

    interface Observable{}

4、 继承自安卓组件的类,采用父类名作为后缀
例如:

class LoginActivity extends Activity{}

5、 自定义异常必须以Exception结尾

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

7、 定义数组时方括号紧随在原始类型之后,数组名称一般使用复数形式
例如:

int[] arrays;

8、 常量、枚举等均采用大写形式,用下划线区分各单词
例如:

final static int DIALOG_ID_ALARM = 1;
enum Season{SPRING, SUMMER, AUTUMN, WINTER};

9、 全局变量添加所有者前缀:实例成员变量前缀m(表示member),类静态变量前缀s(表示static)
例如:
实例变量mRun
类静态变量sInstance

10、 控件变量添加组件前缀,顺序在所有者前缀之后
例如:
全局名称mBtnNext
局部名称btnNext
常见控件前缀如下表:

缩写

全称

含义

缩写

全称

含义

alc

AnalogClock

模拟时钟

rdo

RadioButton

单选按钮

btn

Button

按钮

rtb

RatingBar

评分条

cal

CalendarView

日历

scr

ScrollView

滚动框

chb

CheckBox

复选框

sdr

SlidingDrawer

滑动抽屉

chm

Chronometer

秒表

sfc

SurfaceView

表面视图

dgc

DigitalClock

数字时钟

skb

SeekBar

进度条

dpk

DatePicker

日期选择器

spn

Spinner

下拉框

edt

EditText

编辑框

swh

Switch

开关

els

ExpandableListView

可扩展列表

tbh

TabHost

标签页

glr

Gallery

画廊

tbw

TabWidget

标签按钮

grd

GridView

网络框

tgl

ToggleButton

切换按钮

img

ImageView

图片框

tpk

TimePicker

时间选择器

lst

ListView

列表框

txt

TextView

文本框

mcr

MediaController

多媒体控制

vdo

VideoView

视频

npk

NumberPicker

数字选择器

web

WebView

网页框

rdg

RadioGroup

单选按钮组

lyt

五种基本布局

11、 除单例模式外一般不得使用静态变量

12、 常量一般使用final static修饰,根据需要使用可见性修饰符
例如:
public static final int VISIBLE = 0x00000000; 

13、 一般方法名首字母小写,若由多个单词组成,则其后每个单词首字母大写

14、 构造方法采用递增方式(参数多的写在后面)
例如:

public GameView(Context context) {
this(context, null);
}
public GameView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {}

15、 仅在项目内使用的实体类不使用JavaBean进行封装,直接将成员变量访问修饰符修改为非private
例如:

class User{public String name,pwd;}

16、 实体类中固定值的成员变量可设置成final,并通过构造函数初始化

17、 实体类中不得随意修改的成员变量可添加下划线前缀以作区别
例如:

class User{public int _id;}

18、 一般不使用System.out输出,而是使用Log中的方法

19、 使用BuildConfig.DEBUG标记对Log进行封装,只在调试时输出重要信息,正式版不输出

20、 一般try……catch只捕获需要的异常

21、 catch块不得为空,至少应当将异常信息输出

三、 资源

1、 资源命名全部采用小写,各单词间以下划线区分

2、 布局文件采用[前缀]_[功能模块].xml的命名方式
例如:
MainActivity的布局activity_main.xml
常见前缀如下表:

前缀名称

含义

activity

Activity的主要布局

dialog

自定义对话框的布局

item

适配器视图中每个项目的布局

popup

弹出框的布局

window

悬浮窗的布局

3、 图片采用[性质前缀]_[功能模块]_ [属性后缀].[扩展名]的方式
例如:
主背景图片bg_main.png
确定按钮按下btn_ok_presssed.png
常见属性后缀如下表:

后缀

含义

pressed

按下状态时的图片

disabled

禁用状态下的图片

normal

常规状态下的图片

selector

包含所有状态的选择器

4、 values目录下文件名称较固定,不得随意更改,常见名称如下:

名称

含义

arrays

数组

attrs

自定义属性

colors

自定义颜色

dimens

自定义尺寸

drawables

纯色图片

strings

字符串文件

styles

自定义样式

四、 注释

1、 开源项目必须添加文件注释,非开源项目建议添加
例如:

/*
 * @(#): Document.java
 * @project:IndentObjectNatation
 * @version: v1.1
 * @copyright: Copyright (C) 2013-2014 The Emerald Education
 * @description: 
 * This file is a part of Indent Object Notation project.
 * 
 * @modify:
 * ---- No.1 Modified By Mr. Tang At 2014-05-06 11:32 Based On 1.0 ----
 *      Create this file.
 * ---- No.2 Modified By Mr. Zhang At 2014-05-06 11:32 Based On 1.0 ----
 *      Make the class Document extend from the class Node.
 */

2、 类定义一般需要写类注释,接口一般需要写接口注释,如果没有文件注释,则需要在类注释和接口注释中标出作者
例如:

/**
 * Root of the ION tree, provides the access to the document's data.
 * <p>
 * Subclass of {@link Node}, Since elements, comments, etc. cannot exist
 * outside the context of a Document, the Document also contains the
 * factory methods needed to create these objects.
 * </p>
 * 
 * @author Mr. Zhang
 */
class Document {}

3、 成员变量、静态变量、常量等添加属性注释
例如:

/** This view is invisible. */
public static final int INVISIBLE = 0x00000004;

4、 关联性较大的多个成员变量等可以共用同一条注释
例如:

/** The width and height of View. */
private int mWidth, mHeight;

5、 public和protected方法必须添加方法注释,default和private方法建议添加方法注释
例如:

/**
 * Writes {@code count} characters starting at {@code offset} in {@code buf}
 * to the target.
 *
 * @param buf
 *            the non-null character array to write.
 * @param offset
 *            the index of the first character in {@code buf} to write.
 * @param count
 *            the maximum number of characters to write.
 * @return {@code true} if success, otherwise {@code false}
 * @throws IndexOutOfBoundsException
 *             if {@code offset < 0} or {@code count < 0}, or if {@code
 *             offset + count} is greater than the size of {@code buf}.
 * @throws IOException
 *             if this writer is closed or another I/O error occurs.
 */
public abstract boolean write(char[] buf, int offset, int count) throws IndexOutOfBoundsException, IOException;

6、 若覆盖基类的方法,则可以不写方法注释,但必须用@Override标出
例如:

@Override
protected void onCreate(Bundle savedInstanceState) {}

7、 不建议继续使用的方法用@Deprecated标出

8、 switch……case的每个条件一般添加简短说明
例如:

switch (type) {
case 1:// Android apps
break;
case 2:// Android games
break;
case 3:// iOS apps
break;
default:// Not a valid package
break;
}

9、 如果if的条件大于2个,则必须写注释
例如:

if (isBluetooth // If Bluetooth network is on
|| isWifi // If WLAN network is on
|| is3g // if 3g network is on
) {}

10、 对于未完成的方法,使用TODO加以标记
例如:

void write(byte[] buf, File file) {
// TODO: Write buf to file
}

11、 若功能已完成,但存在效率等潜在问题时,使用XXX加以标记
例如:

void parseXML(File file) {
// XXX: Maybe SAX is better
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
Document doc = builder.parse(file);
}

12、 若代码存在严重问题或仅用于调试,使用FIXME加以标记(注:存在FIXME标记的代码不能作为正式版发布)

boolean login(String name, String pwd) {
//FIXME: Remove this line before publishing
System.out.println("name=" + name + ", password=" + pwd);
if (users.containsKey(name) && users.get(name).equals(pwd))
return true;
return false;
}

13、 如果for、while等代码块过长,可以在结尾处标记循环变量
例如:

for (int position = 0; position < 10; position++) {
……
}// end for: position
while(mRun){
……
}// end while: mRun
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竖子敢尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值