我如何将 char1 从 onCreate 方法传递给另一个方法——在这种情况下,将它传递给 selectChar2?我可以将 char1 和 char2 设为全局吗?我不确定这样做的正确方法是什么。请帮忙!
private String char1,char2; // does this even do anything? I thought this would let me use char1 and char2 anywhere.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_char2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String char1 = (String) extras.getString("char1"); //is the (String) necessary?
TextView textView = (TextView) findViewById(R.id.header);
textView.setText(char1+" vs "+"char2");
}
}
public void selectChar2(View v) {
Button btn = (Button)v;
String char2 = btn.getText().toString();
Intent intent = new Intent(PickChar2.this, DisplayMatchUp.class);
Bundle extras = new Bundle();
extras.putString("Character1",char1); //char1 here is null...how do I get the value from the method above?
extras.putString("Character2", char2);
intent.putExtras(extras);
startActivity(intent);