简体   繁体   中英

how to display value from radio button on edit text?

public class AddAppointment extends AppCompatActivity {
    EditText patname,appday,ages,gender;
    RadioGroup rr;
    RadioButton rr1,rr2;
    
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_appointment);
        gender = findViewById(R.id.gender);
        rr = findViewById(R.id.rr);
        rr1 = findViewById(R.id.rr1);
        rr2 = findViewById(R.id.rr2);
        rr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer id = rr.getCheckedRadioButtonId();
                String g = id.toString();
                if (rr1.isChecked()) {
                    gender.setText(g);
                }else if(rr2.isChecked()){
                    gender.setText(g);
                }
            }
        });

**I am asking for male and female in respective radio button rr1 and rr2, which when any selected should show the result in edittext(gender) **

I'm not sure that I understood your question. Do you want the edit text to show 'Male' when rr1 is checked, and 'Female' when the rr2 is checked?

If so, I'm pretty sure the following code will do just that:

rr.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            // rr1 = Male; rr2 = Female
            if (radioGroup.getCheckedRadioButtonId() == rr1.getId())
                gender.setText("Male");
            else
                gender.setText("Female");
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM