一个类可以同时拥有 getExtra 和 putExtra 吗?假设第 1 类将数据传递给第 2 类。第 2 类将数据传递给第 3 类。我想如何让第 2 类工作?
1级
public static final String EXTRA_RADIO="com.example.flash.Mode";
int btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bP= (Button)findViewById(R.id.btnP);
Button bT= (Button)findViewById(R.id.btnP);
final Intent intent = new Intent(Main.this, Mode.class);
bP.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
btn=1;
intent.putExtra(EXTRA_RADIO, btn);
startActivity(intent);
}
});
bT.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
btn=2;
intent.putExtra(EXTRA_RADIO, btn);
startActivity(intent);
}
});
}
2 级
int mode;
int op;
public static final String EXTRA_OP = "com.example.flash.Operator";
public void onStart(){
super.onStart();
mode = getIntent().getIntExtra(Main.EXTRA_RADIO, 0);
setContentView(R.layout.mode);
if (mode==1){
m.setText("Practice");
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//codes
op=1;
}
});
}
if (mode==2){
m.setText("Trial");
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//codes
op=2;
}
});
}
Intent intent = new Intent(Mode.this, Operator.class);
intent.putExtra(EXTRA_OP, op);
startActivity(intent);
}
然后 3 级从 2 级获得操作。我的 2 级不起作用。