【问题标题】:Sending radio button values in Android在 Android 中发送单选按钮值
【发布时间】:2017-09-06 20:58:22
【问题描述】:

这是我的 AddLift.java:

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

    final EditText Origin = (EditText)findViewById(R.id.addOrigin);
    final EditText Destination = 
(EditText)findViewById(R.id.addDestination);
    final EditText Time = (EditText)findViewById(R.id.setTime);
    final EditText  Seats = (EditText)findViewById(R.id.numOfSeats);
    final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
    final RadioButton selectedButton = 
(RadioButton)findViewById(selectedButton);

    Button submit = (Button)findViewById(R.id.submitButton);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String addOrigin = Origin.getText().toString();
            String addDestination = Destination.getText().toString();
            String setTime = Time.getText().toString();
            String numOfSeats = Seats.getText().toString();
            int selectedId = DriverLifter.getCheckedRadioButtonId();
            selectedButton = (RadioButton)findViewById(selectedId);

            Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
            intent.putExtra("ORIGIN", addOrigin);
            intent.putExtra("DESTINATION", addDestination);
            intent.putExtra("TIME", setTime);
            intent.putExtra("SEATS", numOfSeats);
            intent.putExtra("RADIO", driverLifter);
            startActivity(intent);

        }
    });

这是我的 ViewLiftBoard.java:

public class ViewLiftBoard extends AppCompatActivity {

    String Origin;
    String Destination;
    String Time;
    String Seats;
    int DriverLifter;


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


        Origin = getIntent().getExtras().getString("ORIGIN");
        Destination = getIntent().getExtras().getString("DESTINATION");
        Time = getIntent().getExtras().getString("TIME");
        Seats = getIntent().getExtras().getString("SEATS");
        DriverLifter = getIntent().getExtras().getInt("RADIO");

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText("Origin:"+ " 
        "+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+" 
        "+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);

   }
}

但是当我运行应用程序时,“类型”以数字而不是所选单选按钮的值出现?任何帮助都会很棒

【问题讨论】:

标签: java android android-activity radio-button


【解决方案1】:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_lift);

        final EditText Origin = (EditText)findViewById(R.id.addOrigin);
        final EditText Destination = 
    (EditText)findViewById(R.id.addDestination);
        final EditText Time = (EditText)findViewById(R.id.setTime);
        final EditText  Seats = (EditText)findViewById(R.id.numOfSeats);
        final RadioGroup DriverLifter = (RadioGroup)findViewById(driverLifter);
        final RadioButton selectedButton = 
    (RadioButton)findViewById(selectedButton);

        Button submit = (Button)findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addOrigin = Origin.getText().toString();
                String addDestination = Destination.getText().toString();
                String setTime = Time.getText().toString();
                String numOfSeats = Seats.getText().toString();
                int selectedId = DriverLifter.getCheckedRadioButtonId();
                selectedButton = (RadioButton)findViewById(selectedId);
                String selectedradio=selectedButton.gettex();
                Intent intent = new Intent(AddLift.this, ViewLiftBoard.class);
                intent.putExtra("ORIGIN", addOrigin);
                intent.putExtra("DESTINATION", addDestination);
                intent.putExtra("TIME", setTime);
                intent.putExtra("SEATS", numOfSeats);
                intent.putExtra("RADIO", selectedradio);
                startActivity(intent);

            }
        });



ViewLiftBoard.java:


public class ViewLiftBoard extends AppCompatActivity {

    String Origin;
    String Destination;
    String Time;
    String Seats;
    String DriverLifter;


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


        Origin = getIntent().getExtras().getString("ORIGIN");
        Destination = getIntent().getExtras().getString("DESTINATION");
        Time = getIntent().getExtras().getString("TIME");
        Seats = getIntent().getExtras().getString("SEATS");
        DriverLifter = getIntent().getExtras().getString("RADIO");

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText("Origin:"+ " 
        "+Origin+'\n'+"Destination:"+""+Destination+'\n'+"Time:"+" 
        "+Time+'\n'+"Number of Seats:"+" "+Seats+'\n'+"Type:"+" "+DriverLifter);

   }
}

【讨论】:

    【解决方案2】:

    那是因为您正在从额外的意图中检索 Integer DataType

       DriverLifter= getIntent().getExtras().getInt("RADIO");
    

    要解决你的问题,你必须把这个额外的作为一个字符串放在意图中,所以不要这样做:

     intent.putExtra("RADIO", driverLifter);
    

    这样做:

         intent.putExtra("RADIO",  (RadioButton) findValueById(DriverLifter .getCheckedRadioButton()).  getText(). toString()) ;
    

    然后像这样提取 Extra:

     DriverLifter= getIntent().getExtras().getString("RADIO");
    

    【讨论】:

      【解决方案3】:

      如果你想从选中的单选按钮中获取文本,你可以使用下一个组合:

      RadioGroup driverLifter = (RadioGroup)findViewById(R.id.driverLifter);
      String selectedButtonText = ((RadioButton)findViewById(driverLifter.getCheckedRadioButtonId())).getText().toString();
      

      顺便说一下,根据 Java 命名约定,你的变量应该从小写开始。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-02-07
        • 2013-03-23
        • 2019-11-24
        相关资源
        最近更新 更多