简体   繁体   English

在 Android Studio/Intellij idea 中自动生成 Android Log TAG

[英]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 :最后我想得到下一个结果:

Typing : psfst -> press Ctrl+J -> press Enter输入: psfst -> 按Ctrl+J -> 按Enter

Result :结果 :

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.单击“确定” 、“应用” 、“确定”并使用。

There already exists such shortcut in Android Studio - write logt and enter while cursor is at the class scope. Android Studio 中已经存在这样的快捷方式 - 编写 logt 并在光标位于类范围内时输入。

more here :更多在这里:

更多信息请看这里

For those using Android Studio 3 and Kotlin it is necessary to change how Live Templates set:对于使用Android Studio 3Kotlin 的用户,有必要更改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:脚步:

  1. Add添加
  2. Call logtk are whatever you want to call呼叫 logtk 是您想呼叫的任何内容
  3. Use this code as template text将此代码用作模板文本
private val TAG = this::class.java.simpleName
  1. Add a description增加一个说明
  2. Do it applicable to Kotlin: in my case I used just for Class是否适用于 Kotlin:就我而言,我仅用于 Class

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM