Java 如何使用 log.d 记录整数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27340999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:11:42  来源:igfitidea点击:

how to log an integer value with log.d?

javaandroid

提问by shan

I am very new to Android and am trying some simple log to get a random background color. I have the code and it returns an integer between 1-256, or so I think. I need to log the value to check if it's OK, but I'm not sure how to log it with Android.. I've been using System.out.println("stuff")to log stuff in the past but I believe that's not how you're supposed to do it in Android.

我对 Android 非常陌生,正在尝试一些简单的日志来获取随机背景颜色。我有代码,它返回一个 1-256 之间的整数,我认为是这样。我需要记录该值以检查它是否正常,但我不确定如何使用 Android 记录它.. 我过去一直在使用System.out.println("stuff")记录内容,但我相信这不是你应该做的在安卓中。

I have my class:

我有我的课:

public static int backgroundColorRandomize()

that returns

返回

return randomRGB;

and I try to log it like this

我试着像这样记录它

Log.d(backgroundColorRandomize(), "value = " + randomRGB);

but I need to convert the returned value from backgroundColorRandomizeto a String in order for it to log.

但我需要将返回的值从backgroundColorRandomize转换为字符串才能记录。

I tried java's .toStringbut I'm not sure I'm using it right.. Any help would be appreciated! Thanks!

我尝试过 java 的,.toString但我不确定我是否正确使用它.. 任何帮助将不胜感激!谢谢!

采纳答案by TheRedFox

Log.d("MYINT", "value: " + randomRGB);

回答by Jared Rummler

private static final String TAG = YourClass.class.getSimpleName();

...

android.util.Log.d(TAG, String.format("value = %d. random color = %d", randomRGB, backgroundColorRandomize()));

More info:

更多信息:

Logging libraries: https://android-arsenal.com/tag/57

日志库:https: //android-arsenal.com/tag/57

回答by RestInPeace

Log.d(backgroundColorRandomize() + "" /* <-- all you need. */, "value = " + randomRGB);

回答by code monkey

I prefer String.valueOf(value).

我更喜欢String.valueOf(value)

Log.d(String.valueOf(backgroundColorRandomize()), "value = " + randomRGB);