我有两个活动,第二个和第三个。在 Second 活动中,我有两个单选按钮,红色和蓝色,用户可以使用它们来控制第三活动中的文本颜色。我使用了一个意图和putExtra()
,但它没有按照我的预期运作。
第二项活动:
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btn2 = (Button) findViewById(R.id.go_btn3);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
Intent myIntent = new Intent(Second.this, Third.class);
RadioButton redButton = (RadioButton) findViewById(R.id.color_red);
RadioButton blueButton = (RadioButton) findViewById(R.id.color_black);
myIntent.putExtra("check_red", redButton.isSelected());
myIntent.putExtra("check_blue", blueButton.isSelected());
startActivity(myIntent);
}});
}
第三项活动:
public class Third extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
TextView resultView = (TextView) findViewById(R.id.display);
boolean red_check = getIntent().getExtras().getBoolean("check_red");
resultView.setText(""+red_check); //for testing. what I got is FALSE
}
}
答案 0 :(得分:2)
将isSelected();
来电更改为isChecked();
isSelected()
与isChecked();