我如何开始一个新的Android活动使用字符串? [英] How can I start a new android activity using a string?
问题描述
我在与我的工作在Android应用程序的问题。
I'm having a problem with an android application that I'm working on.
我的应用程序有几个部分,并在下一个屏幕上,加载基于字符串。因此,第1条屏1人,S1S1。
My application has several sections and the next screen that loads is based on a string. So, screen 1 of section 1 would be, S1S1.
我的问题是,我该如何开始基于字符串的活动。我有S1S1保存在一个字符串,可以把它叫做nextactivity。而不是不必键入S1S1.class,我需要它来从字符串。我triend尽我所能的事情和谷歌并没有真正帮助。
My question is, how can I start an activity based on a string. I have S1S1 saved in a string, lets call it nextactivity. Rather than having to type S1S1.class, I need it to come from the string. I've triend everything I can thing of and google hasn't really helped.
有些事情我已经试过是
Intent myIntent = new Intent(nextactivity);
Intent myIntent = new Intent(v.getContext(), getClass().getName().valueOf(nextactivity));
Intent myIntent = new Intent(v.getContext(), Class.forName(nextactivity));
和试图与运行
startActivityForResult(myIntent, 0);
但似乎没有任何工作。任何想法?
but nothing seems to work. Any ideas?
推荐答案
下面是一个code。通过它,你可以用活动的名字开始活动
Here is a code by which you can start activity using the name of the activity
Class<?> c = null;
if(StringClassname != null) {
try {
c = Class.forName(StringClassname );
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent intent = new Intent(mail.this, c);
startActivity(intent);
修改
下面的类名称将是类的包名的全名。
例如,如果你的包的名字将是 XYZ
,如果你有一个名为活动名称 A
活动中的全名 A
将 XYZA
。
Here class name will be full name of the class with the package name.
For example if your package name will be x.y.z
and if you have Activity name called A
then the full name of the Activity A
will be x.y.z.A
.
这篇关于我如何开始一个新的Android活动使用字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!