我有一组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);
}
});
}
});
}
答案 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
}
}
});