简体   繁体   中英

using radiobutton group to open new activity in android studio

I have a group of 4 radiobuttons to select which activity to open when a button is pressed, I have tried the code below for 2 of the radiobuttons, but it is not functioning as intended. I'm completely new to java so any help would be great, thanks!

public class ModeSelect extends ActionBarActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mode_select);

   // Button OpenChannel = (Button)findViewById(R.id.OpenChannelMode0);
    RadioButton SPIM = (RadioButton)findViewById(R.id.SPIMaster);

    SPIM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    Button OpenChannel = (Button)findViewById(R.id.OpenChannelMode0);

    OpenChannel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(ModeSelect.this, SPIOptions.class);
            startActivity(i);
        }
    });
        }
    });

    RadioButton I2CM = (RadioButton)findViewById(R.id.I2CMaster);

    I2CM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            Button OpenChannel = (Button)findViewById(R.id.OpenChannelMode0);

            OpenChannel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(ModeSelect.this, I2COptions.class);
                    startActivity(i);
                }
            });

        }
    });
}

Try in this manner

 Button OpenChannel = (Button)findViewById(R.id.OpenChannelMode0);

    OpenChannel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(radiobuttonObj1.isChecked()){
                //open activity 1
            }else if(radiobuttonObj2.isChecked()){
                //open activity 2
            }

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM