在Android中发送单选按钮值

时间:2017-04-11 12:52:44

标签: java android android-activity radio-button

这是我的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);

   }
}

但是,当我运行应用程序时,"键入"是一个数字而不是所选单选按钮的值?任何帮助都会很棒

3 个答案:

答案 0 :(得分:0)

    @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);

   }
}

答案 1 :(得分:0)

那是因为您正在从intent extra

中检索Integer DataType
   DriverLifter= getIntent().getExtras().getInt("RADIO");

要解决您的问题,您必须将此额外的字符串作为字符串添加到intent中,因此不要这样做:

 intent.putExtra("RADIO", driverLifter);

这样做:

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

并像这样提取Extra:

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

答案 2 :(得分:0)

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

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

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