RadioButtons android发送数据

时间:2016-05-17 13:29:09

标签: java android radio-button radiobuttonlist radio-group

所以我在这里有一个问题。我想实现一个有三个单选按钮的活动

RB1​​

RB2

RB3

还有一个按钮提交。当选择其中任何一个并单击该按钮时,它应该将int值发送到下一个活动。例如,如果选择rb2并且我单击按钮提交,它应该发送int值10.但是如果选择了rb3并且onClick,它应该发送14到下一个Activity。

如何去做?这是我目前的代码

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_btn1:
        if (checked)
            // Send value 10 to the next activity
        break;
    case R.id.radio_btn2:
        if (checked)
            // Send value 14 to the next activity
        break;
     }
}

2 个答案:

答案 0 :(得分:0)

这是一个例子

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_btn1:
        if (checked)
            // Send value 10 to the next activity
        Intent intent = new Intent(this, TheActivityYouWantToStart.class);
        intent.putExtra("number", 10);
        startActivity(intent);
        break;
    case R.id.radio_btn2:
        if (checked)
            // Send value 14 to the next activity
        break;
     }
}

要获得下一个活动中的数字,你必须从这样的意图中获取它

 @Override
    protected void onCreate(Bundle savedInstanceState) {
       int number = getIntent().getIntExtra("number", 999); // 999 is default value
    }

答案 1 :(得分:0)

int valueToBeSent= -1;
switch(view.getId()) {
case R.id.radio_btn1:
    if (checked)
        valueToBeSent = 10;
    break;
case R.id.radio_btn2:
    if (checked)
        valueToBeSent = 14;
    break;
}
if(valueToBeSent!=-1) {
Intent intent = new Intent(_context, NextActivityName.class);
    intent.putExtra("selectedValue", valueToBeSent);
    startActivity(intent);
}