本文实例讲述了android编程实现的自定义注释模板。分享给大家供大家参考,具体如下:
作为一名程序员 不仅要有一个写代码的能力,养成一个良好的编写习惯也是很重要的。今天给大家详细介绍一下如何创建注释模板,给每个类和方法都自己手动去注释信息也可以,只是这样比较繁琐。为何我们不手动去创建一个注释模板呢?
第一: 首先我们需要编写一个模板的xml文件(codetemplates.xml)
第二、我们现在就给xml文件编写主要注释的内容信息,我就直接把自己用的xml信息直接贴出来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>< templates >< template autoinsert = "false" context = "overridecomment_context" deleted = "false" description = "Comment for overriding methods" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.overridecomment" name = "overridecomment" >/** * @描述: * @方法名: ${enclosing_method} * $< li class = "tag item" >< a href = "http://www.zzvips.com/tag/android/1.htm" target = "_blank" title = "搜索关于android的文章" rel = "nofollow" >android</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89/1.htm" target = "_blank" title = "搜索关于自定义的文章" rel = "nofollow" >自定义</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E6%A8%A1%E6%9D%BF/1.htm" target = "_blank" title = "搜索关于模板的文章" rel = "nofollow" >模板</ a ></ li > * @创建人:${user} * @创建时间:${date}${time} * @修改人:${user} * @修改时间:${date}${time} * @修改备注: * @throws */</ template >< template autoinsert = "false" context = "methodcomment_context" deleted = "false" description = "Comment for non-overriding methods" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.methodcomment" name = "methodcomment" >/** * @描述: * @方法名: ${enclosing_method} * $< li class = "tag item" >< a href = "http://www.zzvips.com/tag/android/1.htm" target = "_blank" title = "搜索关于android的文章" rel = "nofollow" >android</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89/1.htm" target = "_blank" title = "搜索关于自定义的文章" rel = "nofollow" >自定义</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E6%A8%A1%E6%9D%BF/1.htm" target = "_blank" title = "搜索关于模板的文章" rel = "nofollow" >模板</ a ></ li > * @返回类型 ${return_type} * @创建人 ${user} * @创建时间 ${date}${time} * @修改人 ${user} * @修改时间 ${date}${time} * @修改备注 * @since * @throws */</ template >< template autoinsert = "false" context = "constructorcomment_context" deleted = "false" description = "Comment for created constructors" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name = "constructorcomment" >/** * <p>Title: </p> * <p>Description: </p> * $< li class = "tag item" >< a href = "http://www.zzvips.com/tag/android/1.htm" target = "_blank" title = "搜索关于android的文章" rel = "nofollow" >android</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89/1.htm" target = "_blank" title = "搜索关于自定义的文章" rel = "nofollow" >自定义</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E6%A8%A1%E6%9D%BF/1.htm" target = "_blank" title = "搜索关于模板的文章" rel = "nofollow" >模板</ a ></ li > */</ template >< template autoinsert = "false" context = "settercomment_context" deleted = "false" description = "Comment for setter method" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.settercomment" name = "settercomment" >/** * @param ${param} ${bare_field_name} */</ template >< template autoinsert = "false" context = "delegatecomment_context" deleted = "false" description = "Comment for delegate methods" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name = "delegatecomment" >/** * $< li class = "tag item" >< a href = "http://www.zzvips.com/tag/android/1.htm" target = "_blank" title = "搜索关于android的文章" rel = "nofollow" >android</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E8%87%AA%E5%AE%9A%E4%B9%89/1.htm" target = "_blank" title = "搜索关于自定义的文章" rel = "nofollow" >自定义</ a ></ li > < li class = "tag item" >< a href = "http://www.zzvips.com/tag/%E6%A8%A1%E6%9D%BF/1.htm" target = "_blank" title = "搜索关于模板的文章" rel = "nofollow" >模板</ a ></ li > * ${see_to_target} */</ template >< template autoinsert = "false" context = "gettercomment_context" deleted = "false" description = "Comment for getter method" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.gettercomment" name = "gettercomment" >/** * @return ${bare_field_name} */</ template >< template autoinsert = "false" context = "typecomment_context" deleted = "false" description = "Comment for created types" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.typecomment" name = "typecomment" >/** * @类描述: * @项目名称:${project_name} * @包名: ${package_name} * @类名称:${type_name} * @创建人:${user} * @创建时间:${date}${time} * @修改人:${user} * @修改时间:${date}${time} * @修改备注: * @version v1.0 * @see [nothing] * @bug [nothing] * @Copyright go3c * @mail *@qq.com */</ template >< template autoinsert = "false" context = "filecomment_context" deleted = "false" description = "Comment for created Java files" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.filecomment" name = "filecomment" >/** * @标题: ${file_name} * @包名: ${package_name} * @功能描述:${todo} * @作者: ${user} * @创建时间: ${date} ${time} * @version v1.0 */</ template >< template autoinsert = "false" context = "fieldcomment_context" deleted = "false" description = "Comment for fields" enabled = "true" id = "org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name = "fieldcomment" >/** * @字段:${field} * @功能描述: * @创建人:${user} * @创建时间:${date}${time} */</ template ></ templates > |
三、我们如何在eclipse中使用这个编写好的xml文件呢? 现在我就给大家解析一下吧!
Window --> Preferences --> Java --> Code Style --> Code Templates --> Comments --> Import --> 选择 codetemplates.xml --> OK 添加后可以自己修改邮箱地址。创建人名称可以在eclipse目录下的eclipse.ini 中增加一行 -Duser.name="whateveryouwant"。
四、我们在eclipse中如何快速使用我们写好的注释模块信息呢?
添加注释时在类名上输入/**然后回车就会自动生成如下格式注释:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * * @类描述: * @项目名称: * @包名: * @类名称:AppDao * @创建人: * @创建时间:2014-4-30上午10:32:30 * @修改人: * @修改时间:2014-4-30上午10:32:30 * @修改备注: * @version v1.0 * @see [nothing] * @bug [nothing] * @Copyright * @mail */ |
在方法名上输入/**然后回车就会自动生成如下格式注释:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * * @描述: * @方法名: UpdatePlayerHistory * @param db * @param item * @return * @返回类型 int * @创建人 * @创建时间 2014-4-30上午10:22:36 * @修改人 * @修改时间 2014-4-30上午10:22:36 * @修改备注 * @since * @throws */ |
描述内容需要自己手动填写。
希望本文所述对大家Android程序设计有所帮助。