Changing one activity to another using RadioButton

2019-09-10 08:55发布

I want to change one activity to another activity. It worked at the first time, But not at the next, How do I fix that?

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条回答
何必那么认真
2楼-- · 2019-09-10 09:17

I noticed that you wanted to go from Activity1 to Activity.class, you cannot do that. Change Activity.class to existing activity, ex: Activity2.class. And you need to simplify your listener: to something like this:

@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);
        }
查看更多
在下西门庆
3楼-- · 2019-09-10 09:22
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);

                }
            }
        });
    }
}
查看更多
登录 后发表回答