我有一个 android 程序,它在单击按钮后发送到另一个活动。主要是,我想在新窗口中设置 textview 中的文本,使其与所选按钮相对应。例如,如果我单击按钮 Writers,下一个新活动应该有一个文本视图,上面会显示单词 Writers。一切正常,除非我尝试在类别的 TextView 图标上设置文本。我还尝试在第一个活动中调用此更改,在启动第二个活动之前,它不起作用。我还提到,如果我用 setText 注释该行,程序就可以正常工作。
private String category;
public final static String CATEGORY_MESSAGE = "e.c.project.CATEGORY";
public void onClick(View v) {
switch(v.getId())
{
case R.id.actors:
category = "actors";
playTheGame(v);
break;
case R.id.cartoons:
category = "cartoons";
playTheGame(v);
break;
case R.id.singers:
category = "singers";
playTheGame(v);
break;
case R.id.writers:
category = "writers";
playTheGame(v);
break;
}
}
public void playTheGame( View view ){
Intent intent = new Intent(this, PlayGame.class);
String category = playGameButton.getText().toString();
intent.putExtra(CATEGORY_MESSAGE, category);
// TextView tv = (TextView) findViewById(R.id.categoryTV);
// tv.setText(category);
startActivity(intent);
}
这是第二个活动的 OnCreate 方法:
private TextView categoryTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);
categoryTV = (TextView) findViewById(R.id.categoryTV);
categoryTV.setText(category);
setContentView(R.layout.activity_play_game);
// Show the Up button in the action bar.
setupActionBar();
}