Android 如何只获取类名,而不是完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10606190/
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
How to get class name only, not full path?
提问by sandalone
I am aware that using Context
and a method getClass().getName()
I can get a string which represent full class name, like com.package1.package2.MainActivity
.
我知道使用Context
和一个方法getClass().getName()
我可以得到一个代表完整类名的字符串,比如com.package1.package2.MainActivity
.
How can I get only the last part, class name only? In this case it would be MainActivity
string.
我怎样才能只得到最后一部分,只有班级名称?在这种情况下,它将是MainActivity
字符串。
I can do it with a simple split()
method, but maybe there is a better way, more reliable.
我可以用一个简单的split()
方法来做,但也许有更好的方法,更可靠。
回答by RobGThai
This is all you need.
这就是你所需要的。
MainActivity.this.getClass().getSimpleName();
回答by sandalone
To get only the name of the class, not full path you will use this expression:
要仅获取类的名称,而不是完整路径,您将使用以下表达式:
String className = this.getLocalClassName();
//or
String className = getBaseContext().getLocalClassName();
//or
String className = getApplicationContext().getLocalClassName();
回答by Yvan Pearson
Alternatively, you could use:
或者,您可以使用:
private static final String TAG = MainActivity.class.getSimpleName();
回答by Rawkode
No matter what way you do it, you'll need to perform an extra operation on top of getClass. I'd recommend this over split:
无论您采用何种方式,您都需要在 getClass 之上执行额外的操作。我推荐这个过度分割:
String className = xxx.getClass();
int pos = className.lastIndexOf ('.') + 1;
String onlyClass = className.substring(pos);
回答by Ashwin
If you need class name within a method, use
getLocalClassName()
If you need class name outside a method, use
getClass().getSimpleName()
If you want to reuse the class name in multiple methods within the same class, then use
private final String TAG = getClass().getSimpleName();
within the class and then use TAG variable in every method.If you want to access the class name from static methods, then use
private static final String TAG = MainActivity.class.getSimpleName();
now use the static variable TAG within your static methods.
如果在方法中需要类名,请使用
getLocalClassName()
如果在方法之外需要类名,请使用
getClass().getSimpleName()
如果要在同一类中的多个方法中重用类名,请在类中使用
private final String TAG = getClass().getSimpleName();
,然后在每个方法中使用 TAG 变量。如果您想从静态方法访问类名,那么
private static final String TAG = MainActivity.class.getSimpleName();
现在使用静态方法中的静态变量 TAG。
回答by Saddan
Use the getSimpleName
method:
使用getSimpleName
方法:
String name = getClass().getSimpleName();
回答by MustafaKhaled
In kotlin you should use:
在 kotlin 中,您应该使用:
val className = javaClass.simpleName
回答by djdance
No other solutions work for me, dunno why. I'm using
没有其他解决方案适合我,不知道为什么。我正在使用
this.getClass().getName().replace("$",".").split("\.")[3]
cons: in one string, and you can use it in threads and listeners.
缺点:在一个字符串中,您可以在线程和侦听器中使用它。
(in listeres I get something like com.djdance.android.main$53)
(在李斯特里我得到类似com.djdance.android.main$53 的东西)
回答by RiccardoCh
If you want to get the name outside the class you can call:
如果你想在课外得到名字,你可以打电话:
MainActivity.class.getSimpleName()
in this way you can avoid to make your class static.
通过这种方式,您可以避免使您的类静态化。