[英]Auto-generate Android Log TAG in Android Studio/Intellij idea
In Intellij Idea when i typing psfs
and then press Ctrl+J IDE was getting me a dialog :在 Intellij Idea 中,当我输入
psfs
然后按Ctrl+J 时, IDE 给我一个对话框:
And when i press Enter i get an当我按Enter 时,我得到一个
I know where i can customize my own output我知道在哪里可以自定义我自己的输出
But i can't any doc's how i can write my own live template.但我无法了解如何编写自己的实时模板。
In the end i want to get next result :最后我想得到下一个结果:
psfst
-> press Ctrl+J -> press Enterpsfst
-> 按Ctrl+J -> 按Enter public static final String TAG = <currentClassName>.class.getSimpleName();
It will be so helpfull, because i have a habit to log my classes.这会很有帮助,因为我有记录课程的习惯。
I find a solution我找到了解决办法
1) Create a new live template in plain group 1)在普通组中创建一个新的实时模板
2) In template text : 2)在模板文本中:
private static final String TAG = $CLASS_NAME$.class.getSimpleName();
3) Define a usage scope : 3)定义使用范围:
4) Choose a shortcut : 4)选择快捷方式:
finally click on Edit variables and change expression value to className()最后点击编辑变量并将表达式值更改为 className()
Click Ok , Apply , Ok and use.单击“确定” 、“应用” 、“确定”并使用。
For those using Android Studio 3 and Kotlin it is necessary to change how Live Templates set:对于使用Android Studio 3和Kotlin 的用户,有必要更改Live Templates 的设置方式:
Editor >> Live Templates >> AndroidLog编辑器 >> 实时模板 >> AndroidLog
By default it is just for Java默认情况下,它仅适用于 Java
Add Kotlin pressing "change" button and check Kotlin添加 Kotlin 按“更改”按钮并检查 Kotlin
and after that it will work again!之后它会再次工作!
Updated: 2020更新时间: 2020
Android Studio: 3.6.2安卓工作室: 3.6.2
Steps:脚步:
private val TAG = this::class.java.simpleName
Update June 2020 2020 年 6 月更新
There's no need to set anything up anymore.没有必要再设置任何东西了。 Android Studio 4.0 comes with this functionality by default.
默认情况下,Android Studio 4.0 带有此功能。 The same with Toast
吐司也是一样
It's super simple in Android studio, just type logt
and press Tab .在 Android Studio 中非常简单,只需输入
logt
并按Tab 即可。
It generates: private static final String TAG = "xyzActivity";
它生成:
private static final String TAG = "xyzActivity";
Another solution is not use a default TAG for each class and use this method to get the TAG:另一种解决方案是不对每个类使用默认 TAG 并使用此方法获取 TAG:
public class Utils {
public static String getTAG(Object o) {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
int position = 0;
for (int i = 0; i < elements.length; i++) {
if (elements[i].getFileName().contains(o.getClass().getSimpleName())
&& !elements[i+1].getFileName().contains(o.getClass().getSimpleName())){
position = i;
break;
}
}
StackTraceElement element = elements[position];
String className = element.getFileName().replace(".java", "");
return "[" + className + "](" + element.getMethodName() + ":" + element.getLineNumber() + ")";
}
}
Example call from MainActivity - onResume:来自 MainActivity 的示例调用 - onResume:
Log.v(Utils.getTAG(this), "hello world");
Log output:日志输出:
[MainActivity](onResume:79): hello world
对于 Kotlin,创建一个实时模板,使用private val TAG = $CLASS_NAME$::class.java.simpleName
作为模板文本,并在 CLASS_NAME 的 set 表达式中作为 kotlinClassName() 在编辑变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.