Android Studio的使用技巧 PS:源码解读
来源:互联网 发布:2017下半年软件设计师 编辑:程序博客网 时间:2024/05/18 00:35
问题描述
根据java中的命名习惯,我们经常会在成员变量名称的前面加个m,比如mBody,mContext,mText等等。有时我们需要写一个get、set方法。那么根据Android studio提供的版本很容易写出如下代码
会出现如下代码:
package wzk.com.slidecloseactivity;/** * @author WangZhengkui on 2016-02-24 12:17 */public class Test { String mText; String mContext; public String getmText() { return mText; } public void setmText(String mText) { this.mText = mText; } public String getmContext() { return mContext; } public void setmContext(String mContext) { this.mContext = mContext; }}
可以看到getmContext,setmContext等方法名,而这种命名方法不符合java命名规范。
解决方案
在上面第二张图片时点击右边的省略号,可以看到生成的上述代码的源代码
public ###if($field.modifierStatic) static ###end$field.type ###set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))#if ($field.boolean && $field.primitive) #if ($StringUtil.startsWithIgnoreCase($name, 'is')) #set($name = $StringUtil.decapitalize($name)) #else is###end#else get###end${name}() { return $field.name;}
虽然不懂这种语法,不过根据生成的代码,我们可以读懂它。
‘##’代表的是连字符
‘#’ 表示的是关键字
‘$’表示的是引用
$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
然后再调用StringUtil中的capitalizeWithJavaBeanConvention()、sanitizeJavaIdentifier()方法获取返回值
StringUtil的源码在http://grepcode.com/file/repository.grepcode.com/java/ext/com.jetbrains/intellij-idea/12.0/com/intellij/openapi/util/text/StringUtil.java这个链接里。
从这里我们知道capitalizeWithJavaBeanConvention()是将变量名的第一个字母大写。
改写步骤
读懂了代码,我们就开始改写。我们的目的是将getmContext()改成getContext()。那我们就需要将m字母去掉。
1、首先点击+号,新建一个模板,命名为remove_m
2、在新模板里将
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
这段代码改成:
#if ($StringUtil.startsWith($helper.getPropertyName($field, $project),"m")) #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($StringUtil.substringAfter($helper.getPropertyName($field, $project),"m"))))#else #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))#end
新添加的代码的意思是如果发现变量名是以”m”开头,则将其移除($StringUtil.substringAfter())。
相应的在Setters模板里,点击+号,新建模板,然后将新模板里的
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName)
改成
#if ($StringUtil.startsWith($helper.getPropertyName($field, $project),"m")) set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($StringUtil.substringAfter($helper.getPropertyName($field, $project),"m")))($field.type $paramName)#else set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName)#end
请注意:替换的内容不包括改行前面的void和末尾的{
我们下次在选择Geters And Setters时选择remove_m即可
package wzk.com.slidecloseactivity;/** * @author WangZhengkui on 2016-02-24 12:17 */public class Test { String mText; String mContext; public String getText() { return mText; } public Test setText(String mText) { this.mText = mText; return this; } public String getContext() { return mContext; } public Test setContext(String mContext) { this.mContext = mContext; return this; }}
至此,大功告成
- Android Studio的使用技巧 PS:源码解读
- ps的一些使用技巧
- Android studio 读源码技巧
- Android Studio使用技巧
- Android Studio使用技巧
- Android Studio 使用技巧
- android studio使用技巧
- android Studio 使用技巧
- android studio 使用技巧
- Android Studio 使用技巧
- Android Studio 使用技巧
- Android Studio 使用技巧
- Android Studio使用技巧
- Android Studio使用技巧
- Android Studio使用技巧
- android studio使用技巧
- Android Studio使用技巧
- Android Studio 使用技巧
- 如何申请CSDN博客?
- 如何解决Android 5.0中出现的警告: Service Intent must be explicit:
- 深度优先搜索求数的全排列
- 类名与需要导入的类的名称重复出错
- Kubernetes基本术语与架构
- Android Studio的使用技巧 PS:源码解读
- TooBar的使用
- Androidx学习笔记(71)--- 需要支付的应用(调用远程的服务)
- ListView显示最后一条Item不完整问题分析
- Androidx学习笔记(72)--- 加载大图片
- Kubernetes集群环境搭建on Centos7
- bzoj1413 取石子游戏 递推
- Android ROM开发(一)——Windows下Cygwin和Android_Kitchen厨房的安装
- 趣味算法-打印数字 (1)