简体   繁体   English

更新单选按钮的值

[英]Updating the value of radio buttons

I've been struggling for couple of days with updating the value of a radio button.几天来,我一直在努力更新单选按钮的值。 I've created the radio button group inside a fragment with two buttons and I need to change the value of the radio button according to which one the user has chosen.我已经在带有两个按钮的片段内创建了单选按钮组,我需要根据用户选择的单选按钮的值来更改单选按钮的值。 This seems simple and straight forward.这看起来简单直接。 The problem is I have to make the radioButton variable final inside the onClick method and as result I can't change its value and if I establish it outside the class I will not be able to access it form inside the class!问题是我必须在 onClick 方法中将 radioButton 变量设为 final,因此我无法更改它的值,如果我在 class 之外建立它,我将无法在类内访问它! Here is my code这是我的代码

enter code here
    // Adding a new consultaion ---------------------
    final TextView titleEditText = rootView.findViewById(R.id.titleEditText);
    final TextView bodyEditText = rootView.findViewById(R.id.bodyEditText);
    final RadioGroup radioGroup = getActivity().findViewById(R.id.radioGroup);
    final int radioButtId = radioGroup.getCheckedRadioButtonId();
    final RadioButton radioButton = getActivity().findViewById(radioButtId);


    final Button sendButt = rootView.findViewById(R.id.sendButt);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.radio_individual:
                    radioButton = rootView.findViewById(R.id.radio_individual);
                    //Toast.makeText(getActivity(), "ind", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.radio_company:
                    radioButton = rootView.findViewById(R.id.radio_company);
                    //Toast.makeText(getActivity(), "com", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    });

And this is the XML code enter code here这是 XML 代码 在此处输入代码

        <RadioButton
            android:id="@+id/radio_individual"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="فرد"
            android:checked="true"

            />
        <RadioButton
            android:id="@+id/radio_company"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="شركة"


             />


    </RadioGroup>

Actually I've got the solution.其实我有解决办法。 First there is not need to use the function radioGroup.setOnCheckedChangeListener all I had to do is to declare the two variables radioButtId and radioButton inside the onClick function of the sending button not outside it.首先不需要使用 function radioGroup.setOnCheckedChangeListener 我所要做的就是在 onClick ZC1C425268E68385D1AB5074C1 之外声明两个变量 radioButtId 和 radioButton

// Adding a new consultation ---------------------
    final TextView titleEditText = rootView.findViewById(R.id.titleEditText);
    final TextView bodyEditText = rootView.findViewById(R.id.bodyEditText);
    final RadioGroup radioGroup = rootView.findViewById(R.id.radioGroup);
    final Button sendButt = rootView.findViewById(R.id.sendButt);


    // Sending button -------------------
    sendButt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Check if fields are empty
            if (titleEditText.getText().toString().matches("") || bodyEditText.getText().toString().matches("")){

            } else {


                // Add the consultaion
                showPD("Sending teh consulation", getActivity());

                // We have to declear the variable of the readio button here!
                final int radioButtId = radioGroup.getCheckedRadioButtonId();
                final RadioButton radioButton = rootView.findViewById(radioButtId);

                ParseObject newCon = new ParseObject("Consultations");
                newCon.put("title", titleEditText.getText().toString());
                newCon.put("body", bodyEditText.getText().toString());
                newCon.put("type", radioButton.getText().toString());
                newCon.put("userPointer", ParseUser.getCurrentUser());

                // Saving the block
                newCon.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            // seccess
                            hidePD();
                            Toast.makeText(getActivity(), "sent", Toast.LENGTH_SHORT).show();    // We use getActivity() instead of HomeFragment.this because we are dealing with a fragment

                        } else {
                            //error
                            hidePD();
                            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();   // We use getActivity() instead of HomeFragment.this because we are dealing with a fragment

                        }
                    }
                });
            }
        }
    });

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

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