============问题描述============
现在就是有一个字符串,例如temp,然后我想通过Intent intent = new Intent(Schedule.this,temp.class);转到temp页面,事先是已经声明好temp类的,怎么做啊
============解决方案1============
try
String temp="package.classname"; Intent intent=new Intent(); intent.setClassName(this, temp);
Intent intent=new Intent(this,Class.forName(temp));
============解决方案2============
楼主 1楼两种方法都可以啊,本人亲测 ,代码如下
package app.example.test1008_0;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {// 点击按钮跳转至Temp页面
@Override
public void onClick(View v) {
// temp值为"包名.类名"
String temp = "app.example.test1008_0.Temp";
Intent intent = null;
try {
intent = new Intent(MainActivity.this, Class.forName(temp));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
startActivity(intent);
}
});
}
}