【问题标题】:Passing RadioButton Value data to another activity将 RadioButton 值数据传递给另一个活动
【发布时间】:2019-11-24 00:57:52
【问题描述】:

我正在尝试将 RadioButton 值数据传递给另一个活动,但我认为代码错误,因为无法在另一个活动的 textview 中查看数据。

这是我在第一个活动 java 中的代码

public class MainActivity extends AppCompatActivity {
EditText age;
RadioGroup jk;
Button send;

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

        jk     = findViewById(R.id.rg);
        age    = findViewById(R.id.age);
        send   = findViewById(R.id.send);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    final Intent intent = new Intent(getApplicationContext(),Next.class);
                    final Bundle b = new Bundle();

                    b.putString("age",age.getText().toString());

                    jk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            if(checkedId == R.id.male){
                                String piljk = getResources().getString(R.string.male);
                                b.putString("gender", piljk);
                            } else {
                                String piljk = getResources().getString(R.string.female);
                                b.putString("gender", piljk);
                            }
                        }
                    });

                    intent.putExtras(b);
                    startActivity(intent);

                }
          }
     }
}

在我的下一个活动 java 文件中

Bundle b = getIntent().getExtras();
        age.setText(b.getCharSequence("age"));
        jk.setText(b.getCharSequence("gender"));

        if (jk.equals("male")){
            int theage = Integer.parseInt(age.getText().toString());
            int HRmax = 220 - theage;
            hrmax.setText(String.valueOf(HRmax));
        } else {
            int theage = Integer.parseInt(age.getText().toString());
            int HRmax = 226 - theage;
            hrmax.setText(String.valueOf(HRmax));
        }

任何帮助将不胜感激。谢谢~

【问题讨论】:

    标签: java android android-studio radio-button


    【解决方案1】:

    尝试获取 button 中的 position,即 checked:

     send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    final Intent intent = new Intent(getApplicationContext(),Next.class);
                    final Bundle b = new Bundle();
    
                    b.putString("age",age.getText().toString());
    
                    int checked = jk.getCheckedRadioButtonId();
                    b.putString("checked", checked);
    
    
                    intent.putExtras(b);
                    startActivity(intent);
    
                }
            }
    

    在您的下一个活动 java 文件中:

    Bundle b = getIntent().getExtras();
            age.setText(b.getCharSequence("age"));
            int checked = b.getInt("checked");
    
    
             if (checked==1){ // male
                        jk.setText("male");
                        int theage = Integer.parseInt(age.getText().toString());
                        int HRmax = 220 - theage;
                        hrmax.setText(String.valueOf(HRmax));
                    } else {// female
                        jk.setText("female");
                        int theage = Integer.parseInt(age.getText().toString());
                        int HRmax = 226 - theage;
                        hrmax.setText(String.valueOf(HRmax));
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      相关资源
      最近更新 更多