我一直在网上搜索有关可以包含类对象的变量的线索,但我什么也没找到。我相信有某种方法可以将有关类的信息存储在变量中以供 Intent 使用。抱歉,解释不好,这是说明我的意思的代码。
MainActivity.class
public class MainActivity extends Activity {
Button startBtn, chooseLevelBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn = (Button) findViewById(R.id.startBtn);
chooseLevelBtn = (Button) findViewById(R.id.chooseLevelBtn);
class classVariable = "Level001Activity.class";
/*
* I want a variable like this where I can store the Activity.class
* So that I can use a function to determine the last level played,
* and so when the player press playBtn, it would automatically
* send the intent to the latest level
* Now, how do I achieve this ?
*/
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
classVariable);
startActivity(i);
}
});
}
}
P.S 我将从存储在设备内部存储中的 json 文件中获取最后玩过的关卡信息。或者任何更好的方法将不胜感激。预先感谢您。
最佳答案
是的,你可以做到。您需要将其设为 Class
而不是 class
并删除引号,因为它不应该是 String
:
Class<? extends Activity> classVariable = Level001Activity.class;
关于java - 如何创建一个包含要在 Intent 中使用的类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32973214/