如何在两个活动之间传递信息?

时间:2013-11-21 22:55:55

标签: android-intent android-ui

我有两个活动,第二个第三个​​。在 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
    }
}

1 个答案:

答案 0 :(得分:2)

isSelected();来电更改为isChecked();

isSelected()isChecked();

的含义完全不同