将radiobutton的数据从一个活动发送到另一个活动

时间:2016-09-07 14:10:18

标签: java android android-intent android-activity

我有两个名为MainActivity和DisplayColorsActivity的活动。 MainActivity将选择DisplayColorsActivity上按钮的颜色。

示例运行:单击绿色表示按钮1,红色单击按钮2,然后单击确定按钮。

MainActivity:

选择按钮1的颜色: oBlue oGreen 或运算

选择按钮2的颜色: oBlue oGreen 或运算

[确定按钮]

DisplayColorsActivity:

[返回按钮] //this should be color Green [重置按钮] //this should be color Red

我的问题是,当单击“确定”按钮时,我的MainActivity不会转到DisplayColorsActivity。任何帮助将不胜感激。谢谢!

我的MainActivity.java

public class MainActivity extends Activity {

private Button okButton;
private RadioGroup colors;
private String color = "#0000ff";
Intent selectedColor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    selectedColor = new Intent();

    okButton = (Button) findViewById(R.id.buttonOk);
    colors = (RadioGroup) findViewById(R.id.radioGroupColors1);

    colors.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
                case R.id.radioRed:
                    color = "#ff0000";
                    break;
                case R.id.radioBlue:
                    color = "#0000ff";
                    break;
                case R.id.radioGreen:
                    color = "#00ff00";
                    break;
            }
        }
    });

    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Intent intent = new Intent(this, DisplayColorsActivity.class);
        intent.putExtra("selectedColor", color);
        setResult(RESULT_OK, intent);
        startActivity(intent);
        finish();
        }
    });
}

我的DisplayColorsActivity.java:

public class DisplayColorsActivity extends Activity {

private String color = "#ff0000";
private static final  int COLOR_SELECTOR=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.colorselector);
    Button selectColorButton = (Button) findViewById(R.id.returnButton);

    selectColorButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent request =new Intent(DisplayColorsActivity.this, MainActivity.class);
            startActivityForResult(request, COLOR_SELECTOR);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch(requestCode){
        case COLOR_SELECTOR:
            if(resultCode == RESULT_OK){
                String color= data.getExtras().getString("selectedColor");
                findViewById(R.id.returnButton).setBackgroundColor(Color.parseColor(color));
            }
    }
}

修改 当我点击MainActivity中的Ok按钮时,DisplayColorsActivity已经显示,但它没有显示我选择的颜色。

2 个答案:

答案 0 :(得分:0)

您的第二项活动应该是: -

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class DisplayColorsActivity extends Activity {

    private String color = "#ff0000";
    private static final  int COLOR_SELECTOR=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.colorselector);
        Button selectColorButton = (Button) findViewById(R.id.returnButton);

  String color= getIntent().getExtras().getString("selectedColor");
                    selectColorButton.setBackgroundColor(Color.parseColor(color));


 selectColorButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent request =new Intent(DisplayColorsActivity.this, MainActivity.class);
                startActivity(request);
            }
        });
    }

答案 1 :(得分:-1)

尝试在#finish

之后致电#setResult