final Button voteYes = (Button) findViewById(R.id.voteYes);
voteYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent successPhase = new Intent(player1.this, player5.class);
startActivity(successPhase);
}
});
我有一个整数变量,范围从1到5,表示int数。所以,我的问题是,如果我想让它成为播放器[数字] .class,而不是修复" player5.class",我该怎么办? (而不是硬编码" 5",我希望它是"数字")
答案 0 :(得分:0)
可以执行以下操作,例如:
Intent intent = new Intent();
String myClassName = "player" + number; // use full package of class
intent.setClassName(context, myClassname;
startActivity(intent);
答案 1 :(得分:0)
Class players [] = {Player1.class, Player2.class,Player3.class, Player4.class,Player5.class};
像这样使用:
Intent successPhase = new Intent(this, players[number]);
startActivity(successPhase);