简体   繁体   English

如何在编辑文本上显示单选按钮的值?

[英]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) ** **我在各自的单选按钮 rr1 和 rr2 中要求男性和女性,当任何选择时应在 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?您是否希望编辑文本在选中 rr1 时显示“男性”,而在选中 rr2 时显示“女性”?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从单选按钮获取值并显示在另一个片段的编辑文本上? - how to get value from radio button and display on edit text on another fragment? 如何获取单选按钮的值并在文本视图上显示(Java for android) - how to get value of radio button and display on text view (Java for android) 如何在选择单选按钮上显示文本字段 - how to display text fields on select the radio button 如何从按钮获取文本以编辑文本? - How to get text from button to edit text? 如何将单选按钮中的字符串保存在arraylist中,并在文本框中显示在下一页 - How should I save string from the Radio Button in arraylist and display it on next page in text box 更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本 - How to get the text from radio button in a radio group when radio button checked state is changed 如何根据先前的值显示选中的单选按钮? - How to display checked radio button based on previous value? 如何根据单选按钮组件的值验证输入文本组件 - How to validate an input text component based on the value of a radio button component 让所选的单选按钮显示在文本区域中 - Have the selected radio button to display in text area 如果选中单选按钮,则在 Java 中显示一个文本框 - If radio button is checked, display a text box in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM