更新单选按钮的值

huangapple 未分类评论42阅读模式
英文:

Updating the value of radio buttons

问题

我已经挣扎了几天,试图更新单选按钮的值。我已经在一个片段中创建了包含两个按钮的单选按钮组,我需要根据用户选择的按钮来更改单选按钮的值。这似乎很简单和直接。问题是我必须在onClick方法内部将radioButton变量声明为final,因此我无法更改它的值;如果我将其声明在类的外部,我将无法从类的内部访问它!以下是我的代码:

// 添加新的咨询 ---------------------
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;
        }
    }
});

以下是XML代码:

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="30dp">

    <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>
英文:

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!
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(), &quot;ind&quot;, Toast.LENGTH_SHORT).show();
                    break;
                case R.id.radio_company:
                    radioButton = rootView.findViewById(R.id.radio_company);
                    //Toast.makeText(getActivity(), &quot;com&quot;, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    });

And this is the XML code
enter code here
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="30dp">

        &lt;RadioButton
            android:id=&quot;@+id/radio_individual&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;فرد&quot;
            android:checked=&quot;true&quot;

            /&gt;
        &lt;RadioButton
            android:id=&quot;@+id/radio_company&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;شركة&quot;


             /&gt;


    &lt;/RadioGroup&gt;

答案1

得分: 0

实际上我已经找到了解决方案首先不需要使用函数radioGroup.setOnCheckedChangeListener我只需要在发送按钮的onClick函数内部声明两个变量radioButtId和radioButton而不是在外部声明

// 添加新咨询 ---------------------
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);

// 发送按钮 -------------------
sendButt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // 检查字段是否为空
        if (titleEditText.getText().toString().matches("") || bodyEditText.getText().toString().matches("")) {

        } else {
            // 添加咨询
            showPD("发送咨询中", getActivity());

            // 在这里声明单选按钮的变量!
            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());

            // 保存数据块
            newCon.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        // 成功
                        hidePD();
                        Toast.makeText(getActivity(), "已发送", Toast.LENGTH_SHORT).show();    // 使用getActivity()而不是HomeFragment.this,因为我们处理的是片段

                    } else {
                        // 出错
                        hidePD();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();   // 使用getActivity()而不是HomeFragment.this,因为我们处理的是片段

                    }
                }
            });
        }
    }
});
英文:

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.

// 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(&quot;&quot;) || bodyEditText.getText().toString().matches(&quot;&quot;)){

            } else {


                // Add the consultaion
                showPD(&quot;Sending teh consulation&quot;, 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(&quot;Consultations&quot;);
                newCon.put(&quot;title&quot;, titleEditText.getText().toString());
                newCon.put(&quot;body&quot;, bodyEditText.getText().toString());
                newCon.put(&quot;type&quot;, radioButton.getText().toString());
                newCon.put(&quot;userPointer&quot;, ParseUser.getCurrentUser());

                // Saving the block
                newCon.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            // seccess
                            hidePD();
                            Toast.makeText(getActivity(), &quot;sent&quot;, 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

                        }
                    }
                });
            }
        }
    });

huangapple
  • 本文由 发表于 2020年4月4日 22:06:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/61029315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定