使用Android Studio 1.1.0。这就是我想要完成的......
我有一个屏幕设置,可以收集游戏中的玩家数量。基于该微调器的值,我想在下一个活动中显示X个文本框以捕获玩家名称。
我该如何设置?
答案 0 :(得分:0)
您可以通过在Intent中将它们设置为Extra来在Activity之间传递数据。本问题解释了如何:How do I create an android Intent that carries data?
如果你有一个List,你可以在其中存储玩家名称的字符串,你可以这样做,例如:
Intent intent = new Intent();
intent.putStringArrayListExtra("playerNames", yourList);
在您的下一个Activity中,您可以创建一个ListView(或GridView或您认为适合此任务的任何内容),在其中显示所有播放器名称。
答案 1 :(得分:0)
首先从您的第一个活动传递此信息:
int numberOfPlayers;
获取旋转器中的玩家数量,但是您目前这样做会将其设置为numberOfPlayers
然后在开始新活动时传递此内容
Intent getPlayerNamesIntent = new Intent(MainActivity.this, PLayerNamesActivity.class);
getPlayerNamesIntent.putExtra("NUM_PLAYERS", numberOfPlayers);
startActivity(getPlayerNamesIntent);
然后在下一个活动onCreate中获取你的额外内容
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Layout should create have something like LinearLAyout with orientation vertical with name android:id="@+id/linearLayoutParent"
setContentView(R.layout.base_layout);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutParent);
// get the NUmber from extras
int numberOfPlayers = getIntent().getExtras().getInt("NUM_PLAYERS");
if(numberOfPlayers > 0){
for(int i = 0; i < numberOfPlayers; i++){
EditText editText = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
// Set Tag here, so you can use tag to get the right player later
editText.setTag("PlayerNumber_" + Integer.toString(i));
layout.addView(editText);
}
}
}