使用RadioButton将一个活动更改为另一个活动

时间:2016-08-17 10:13:07

标签: android android-studio android-intent

我想将一项活动更改为另一项活动。它在第一次工作,但不是在下一次,我该如何解决?

public class Activity1 extends Activity {

    RadioGroup radioGroup;
    RadioButton Rd1, Rd2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity1);

    radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
    Rd1=(RadioButton)findViewById(R.id.radioButton);
    Rd2=(RadioButton)findViewById(R.id.radioButton2);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(Rd1.isChecked())
                {
                    Intent intent = new Intent(getApplicationContext(), Acitivity.class);
                    startActivity(intent);

                }
                else {
                    if (Rd2.isChecked()) {
                        Intent intent1 = new Intent(getApplicationContext(), Acivity1.class);
                        startActivity(intent1);

                    }
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

public class Activity1 extends Activity {

    RadioGroup radioGroup;
    RadioButton Rd1, Rd2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity1);

    radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
    Rd1=(RadioButton)findViewById(R.id.radioButton);
    Rd2=(RadioButton)findViewById(R.id.radioButton2);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.radioButton)
                {
                    Intent intent = new Intent(getApplicationContext(), Acitivity.class);
                    startActivity(intent);

                }
                else 
                 if (checkedId == R.id.radioButton2 ) {
                        Intent intent1 = new Intent(getApplicationContext(), Acivity1.class);
                        startActivity(intent1);

                }
            }
        });
    }
}

答案 1 :(得分:0)

我注意到你想从Activity1转到Activity.class,你不能这样做。将Activity.class更改为现有活动,例如:Activity2.class。你需要简化你的听众:这样的事情:

@Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Intent intent = null;
            if (Rd1.isChecked()) {
                intent = new Intent(Activity1.this, Activity2.class);
            } else if(Rd2.isChecked()) {
                intent = new Intent(Activity1.this, Activity3.class);
            }
            startActivity(intent);
        }