【问题标题】:RadioButtons android sending dataRadioButtons android发送数据
【发布时间】:2016-09-13 15:23:59
【问题描述】:

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

rb1

rb2

rb3

还有一个按钮提交。 When any one of those are selected and the button is clicked, it should send an int value to the next activity.例如,如果选择了 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;
     }
}

【问题讨论】:

  • 你知道Intents吗?
  • 是的,我知道意图

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


【解决方案1】:

这是一个例子

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

要在下一个活动中获取数字,您必须像这样从 Intent 获取它

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

【讨论】:

    【解决方案2】:
    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2013-02-24
      相关资源
      最近更新 更多