大家好,又见面了,我是你们的朋友全栈君。
序言
在使用android studio编写代码的时候,有一些模版功能能够增加开发效率,例如,当你输入logd
的时候,会出现如下图选项:
然后选择这一项按下enter键,就会自动出现如下代码:
Log.d(TAG, "onCreate: ");
复制代码
并且光标定位在冒号后面,这样就能方便的增加日志信息,下面来介绍一下该功能。
添加自己的模版
刚才的日志输出后的TAG没有定义,现在来创建一个TAG的模版。
进入android studio的设置界面后,然后打开如下地方:
然后点击面板右上方的家好店,就可以分别创建模版及模版组(模版指logd,模版组指AndroidLog),先选择user组,然后选择创建模版,在Abbreviation处填写输入唤起模版的关键词,对应于日志模版的 logd
,我们在这儿输入 tag
,Description处填写这一个模版的描述信息,在这儿填写 public static final String TAG = "";
,然后在下面的内容区域填写之后模版要生成的内容,填写上 public static final String TAG = "$className$";
,这时面板如下:
其中className变颜色了,这时模版的语法,使用两个$来标记这是带填充的内容,然后点击右侧的 Edit variables 按钮,为带填充的内容选择内容:
每一项都很好理解,我们在表达式处选择 className()
,然后点击下方的 Define
来设置这个模版能使用的地方。
我们选择java下面的 declaration,这样他能在java的class下面和字段同级的地方使用。
然后我们在class声明属性的地方输入tag:
按下enter就直接出现了如下代码:
public static final String TAG = "MainActivity";
复制代码
如此我们就成功添加了一个使用的自定义模版。
模版功能的更多介绍
在模版里输入双美元符号包裹的属性事,如果属性名输入相同的,最后能生成相同的值。如果不为属性选择函数,就需要自己去输入值,其的用法可以参见下一节的 快速生成retrofit的field注解 。
下面列举一些常用的属性的生成函数:
函数名 | 函数值 |
---|---|
user() | 当前计算机的用户名 |
date() | 当前时间 |
methodName() | 当前方法名 |
methodParameters() | 当前方法的输入参数 |
methodReturnType() | 当前方法的返回值类型 |
className() | 当前类的类名 |
classNameComplete() | 当前类的完整类名 |
还有一些其他的可以自行探索。
一些实用模版
下面介绍几个实用的模版。
页面的根布局
在xml文件里快速生成以LinearLayout为根布局的模版:
使用 DataBinding
的版本
供于测试的时候快速生成main方法的模版
Activity头部的注释
快速生成retrofit的field注解
可以将如下配置文件复制,保存为user.xml,放在android studio的配置目录里面直接引入这些模版,其中mac保存在 user/{用户名}/资源库/Preferences/AndroidStudio3.2/templates 下面,Windows保存在C盘下 .AndroidStudio3.2 的相应位置。
<templateSet group="user">
<template name="noteActivity" value="/** * Activity描述信息: * <p> * 需要的intent参数: * * @author $USER$ * @date $DATE$ */ " description="Activity头部注释" toReformat="false" toShortenFQNames="true">
<variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
<variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="api" value="/** * 描述: * @param $param$ * @return $return$ * @exception $exception$ * @see IOException */" description="api注释" toReformat="false" toShortenFQNames="true">
<variable name="param" expression="methodParameters()" defaultValue="" alwaysStopAt="true" />
<variable name="return" expression="methodReturnType()" defaultValue="" alwaysStopAt="true" />
<variable name="exception" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="fbc" value="($cast$) findViewById(R.id.$resId$);" description="findViewById with cast" toReformat="false" toShortenFQNames="true" deactivated="true">
<variable name="cast" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="resId" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_EXPRESSION" value="true" />
</context>
</template>
<template name="retrofitQuery" value="@Query("$name$") String $name$" description="retrofit Query参数" toReformat="false" toShortenFQNames="true">
<variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="retrofitField" value="@Query("$name$") String $name$" description="retrofit Field参数" toReformat="false" toShortenFQNames="true">
<variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="syso" value="System.out.println($message$);" description="System.out.println();" toReformat="false" toShortenFQNames="true">
<variable name="message" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_STATEMENT" value="true" />
</context>
</template>
<template name="tag" value="public static final String TAG = "$className$";" description="public static final String TAG = "";" toReformat="false" toShortenFQNames="true">
<variable name="className" expression="className()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="noteContentProvider" value="/** * ContentProvider描述信息: * <p> * * @author $USER$ * @date $DATE$ */ " description="ContentProvider头部注释" toReformat="false" toShortenFQNames="true">
<variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
<variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="noteService" value="/** * Service描述信息: * <p> * * * @author $USER$ * @date $DATE$ */ " description="Service头部注释" toReformat="false" toShortenFQNames="true">
<variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
<variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="noteBroadcastReceiver" value="/** * BroadcastReceiver描述信息: * <p> * 监听的广播: * * @author $USER$ * @date $DATE$ */ " description="BroadcastReceiver头部注释" toReformat="false" toShortenFQNames="true">
<variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
<variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="noteCommon" value="/** * 类描述信息: * * @author $USER$ * @date $DATE$ */ " description="java类头部注释" toReformat="false" toShortenFQNames="true">
<variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
<variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="layoutRoot DataBinding" value="<layout> <data> </data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </layout>" description="页面的初始布局 DataBinding" toReformat="false" toShortenFQNames="true">
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="layoutRoot" value="<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> " description="页面的初始布局" toReformat="false" toShortenFQNames="true">
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="main" value=" public static void main(String[] args) { }" description="main方法" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
<template name="mode" value="<input type="button" name="$method$" value="$method$" onClick="$method$();" /> function $method$() { $("result").innerText = "$method$"; var ret = HardwareInfo.$method$(); $("result").innerText = "$method$成功" + ret; } " description="模板" toReformat="false" toShortenFQNames="true">
<variable name="method" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
</templateSet>
复制代码
转载于:https://juejin.im/post/5c7caabd51882540ca1ccfbe
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106956.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...