使用单选按钮组在android studio中打开新活动

时间:2015-02-24 12:47:08

标签: java android android-activity radio-button

我有一组4个单选按钮,用于选择按下按钮时要打开的活动,我已经尝试了下面的2个无线电按钮的代码,但它没有按预期运行。我是java的新手,所以任何帮助都会很棒,谢谢!

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);
                }
            });

        }
    });
}

1 个答案:

答案 0 :(得分:1)

以这种方式尝试

 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
            }

        }
    });