看过上一篇文章: 使用AS - Live Template 添加代码模板 的朋友知道可以通过 AS 的 Live Template 添加代码模板,但是那么我们要添加一个 class 类模板呢
那么这次我们就需要用到 AS 里面的 File Template 了
file 这个 tab 我们可以添加 class 模板,在 other 中有系统已经定义好的 class 模板,我们常用的 activity ,fragment 模板都在里面,我们可以进去手动修改成我们自己需要的样子,比如让 activity 默认 extends AppCompatActivity
include 这个 tab 里面我们可以自定义 class 的注解样式
自定义新建一个 class 模板出来
我们先来一个简单的,随便建一个 class 出来,这个模板叫 AAA
然后我们在 new 一个 class 时可以在 2 处地方选择我们建的模板
File Template 内置参数
我们可以看到在上面我们在定义 AAA 这个模板时使用了几个内置参数:
- ${PACKAGE_NAME} 包名
- ${NAME} 类名
- 使用:$ + {} 来引用这些内置参数
内置参数还有很多的,那么我们去哪里可以看到相关文档呢。在 File 这个 tab 的下面有一个 Singleton 模板,里面就有详细的内置参数说明
内置参数 | 说明 |
---|---|
${PACKAGE_NAME} | name of the package in which the new file is created |
${NAME} | name of the new file specified by you in the New <TEMPLATE_NAME> dialog |
${USER} | current user system login name |
${DATE} | current system date |
${TIME} | current system time |
${YEAR} | current year |
${MONTH} | current month |
${MONTH_NAME_SHORT} | first 3 letters of the current month name. Example: Jan, Feb, etc. |
${MONTH_NAME_FULL} | full name of the current month. Example: January, February, etc. |
${DAY} | current day of the month |
${DAY_NAME_SHORT} | first 3 letters of the current day name. Example: Mon, Tue, etc. |
${DAY_NAME_FULL} | full name of the current day. Example: Monday, Tuesday, etc. |
${HOUR} | current hour |
${MINUTE} | current minute |
${PROJECT_NAME} | the name of the current project |
详细的如下:此处有 bug 可能会出现2次
内置参数 | 说明 |
---|---|
${PACKAGE_NAME} | name of the package in which the new file is created |
${NAME} | name of the new file specified by you in the New <TEMPLATE_NAME> dialog |
${USER} | current user system login name |
${DATE} | current system date |
${TIME} | current system time |
${YEAR} | current year |
${MONTH} | current month |
${MONTH_NAME_SHORT} | first 3 letters of the current month name. Example: Jan, Feb, etc. |
${MONTH_NAME_FULL} | full name of the current month. Example: January, February, etc. |
${DAY} | current day of the month |
${DAY_NAME_SHORT} | first 3 letters of the current day name. Example: Mon, Tue, etc. |
${DAY_NAME_FULL} | full name of the current day. Example: Monday, Tuesday, etc. |
${HOUR} | current hour |
${MINUTE} | current minute |
${PROJECT_NAME} | the name of the current project |
当然我们也可以自己声明一个参数出来,写法和系统的内置参数一样,注意要大写,我们给 AAA 改改
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
public class ${NAME} {
public static final String TAG = "AAA";
public static final String ${TAG_NAME} = "${TAG_VALUE}";
}
我们来一个复杂的
我们上一个 adapter 的模板出来,哇哈哈哈,这个可是很贴合实际啦,这个学会啦,以后写代码快快哒。
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* 文 件 名: ${NAME}
* 作 者: zhao
* 创建日期: ${DATE} ${HOUR}:${MINUTE}
*/
#parse("File Header.java")
public class ${NAME} extends RecyclerView.Adapter<${NAME}.${VIEWHOLDER_CLASS}> {
private final Context context;
private List<${ITEM_CLASS}> items;
public ${NAME}(List<${ITEM_CLASS}> items, Context context) {
this.items = items;
this.context = context;
}
@Override
public ${VIEWHOLDER_CLASS} onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.${LAYOUT_RES_ID}, parent, false);
return new ${VIEWHOLDER_CLASS}(v);
}
@Override
public void onBindViewHolder(${VIEWHOLDER_CLASS} holder, int position) {
${ITEM_CLASS} item = items.get(position);
//TODO Fill in your logic for binding the view.
}
@Override
public int getItemCount() {
if (items == null){
return 0;
}
return items.size();
}
public static class ${VIEWHOLDER_CLASS} extends RecyclerView.ViewHolder {
public ${VIEWHOLDER_CLASS}(View contentView) {
super(contentView);
}
}
}
生成的代码就不贴了,都能知道的。不知道大家发现没有, File Template 一次只能生成一个 class ,我们要是想在公司的 MVP/MVVM 框架规范下,一次性新建一个 UI 页面及其附属 class 怎么办,这可是好几个 class 了,没关系,先看下一篇,比 File Template 复杂的多。