При нажатии кнопки выбранное состояние RadioButton в RadioGroup должно быть сохранено в SharedPreferences с использованием фрагментов.

Фрагмент Activity, который содержит RadioGroup с 5 RadioButtons и Button.RadioButton5, имеет EditText с ним.

public void onCreate(Bundle savedInstanceState) {

    radio1 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio1", false);
    radio2 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio2", false);
    radio3 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio3", false);
    radio4 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio4", false);
    radio5 = PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("radio5", false);
    str_rbText = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("Selected_rb_msg", "nomi");
    msgToSend = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("msgToSend", "");
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    viewRoot =  inflater.inflate(R.layout.fragment_messages, container, false);

    radioGroup = (RadioGroup) viewRoot.findViewById(R.id.radio_grp);
    rb1 = (RadioButton) viewRoot.findViewById(R.id.rb_msg1);
    rb2 = (RadioButton) viewRoot.findViewById(R.id.rb_msg2);
    rb3 = (RadioButton) viewRoot.findViewById(R.id.rb_msg3);
    rb4 = (RadioButton) viewRoot.findViewById(R.id.rb_msg4);
    rb5 = (RadioButton) viewRoot.findViewById(R.id.rb_customMsg);
    editText = (EditText) viewRoot.findViewById(R.id.et_customMsg);

    if(radio1){
        rb1.setChecked(true);
    }
    else if(radio2){
        rb2.setChecked(true);
    }
    else if(radio3){
        rb3.setChecked(true);
    }
    else if(radio4){
        rb4.setChecked(true);
    }
    else if(radio5){
        rb5.setChecked(true);
        editText.setVisibility(View.VISIBLE);
        editText.setHint(PreferenceManager.getDefaultSharedPreferences(getActivity()).ge
tString("Selected_rb_msg", ""));
    }

    function();
    return viewRoot;
}
public void function(){

    SharedPreferences sp = 
PreferenceManager.getDefaultSharedPreferences(getActivity());
    final SharedPreferences.Editor editor = sp.edit();
    if(rb1.isChecked() && !rb5.isChecked() && !rb2.isChecked() && 
!rb3.isChecked() && !rb4.isChecked()){
        editText.setVisibility(View.INVISIBLE);
    }

radioGroup.setOnCheckedChangeListener(new 
RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_msg1){
            str_rbText = rb1.getText().toString();
            editText.setVisibility(View.INVISIBLE);
            editText.setText("");
            //editor.putBoolean("radio1", true).apply();
        } else if (checkedId == R.id.rb_msg2){
            str_rbText = rb2.getText().toString();
            editText.setVisibility(View.INVISIBLE);
            editText.setText("");
            //editor.putBoolean("radio2", true).apply();
        } else if (checkedId == R.id.rb_msg3){
            str_rbText = rb3.getText().toString();
            editText.setVisibility(View.INVISIBLE);
            editText.setText("");
            //editor.putBoolean("radio3", true).apply();
        } else if (checkedId == R.id.rb_msg4){
            str_rbText = rb4.getText().toString();
            editText.setVisibility(View.INVISIBLE);
            editText.setText("");
            //editor.putBoolean("radio4", true).apply();
        } else if (checkedId == R.id.rb_customMsg){
                editText.setVisibility(View.VISIBLE);
                editText.setHint("Enter your custom message");
                str_rbText = editText.getEditableText().toString().trim();
        }
    });

onClicking button состояние выбранного переключателя должно сохраняться в SharedPreferences и загружаться при запуске действия

Button btnSet = (Button) viewRoot.findViewById(R.id.btn_set_msgFrag);
    btnSet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("SelectedRadioButton",str_rbText);

            int id = radioGroup.getCheckedRadioButtonId();
            if(id == R.id.rb_msg1){
                editor.putBoolean("radio1", true).apply();
            }else if(id == R.id.rb_msg2){
                editor.putBoolean("radio2", true).apply();
            }else if(id == R.id.rb_msg3){
                editor.putBoolean("radio3", true).apply();
            }else if(id == R.id.rb_msg4){
                editor.putBoolean("radio4", true).apply();
            }else if(id == R.id.rb_customMsg){
                editor.putBoolean("radio5", true).apply();
            }
        }
    });
}
}

Состояние RadioButton не сохраняется правильно. Состояние 1-го RadioButton всегда истинно при получении при вызове этого действия.


person Noman Arif    schedule 10.09.2017    source источник


Ответы (1)


При установке одного состояния выбранного radioButton поставьте false в другие radioButtons.

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.rb_msg1){
                str_rbText = rb1.getText().toString();
                editText.setVisibility(View.INVISIBLE);
                editText.setText("");
                editor.putBoolean("radio1", true).apply();
                editor.putBoolean("radio2", false).apply();
                editor.putBoolean("radio3", false).apply();
                editor.putBoolean("radio4", false).apply();
                editor.putBoolean("radio5", false).apply();
            } else if (checkedId == R.id.rb_msg2){
                str_rbText = rb2.getText().toString();
                editText.setVisibility(View.INVISIBLE);
                editText.setText("");
                editor.putBoolean("radio2", true).apply();
                editor.putBoolean("radio1", false).apply();
                editor.putBoolean("radio3", false).apply();
                editor.putBoolean("radio4", false).apply();
                editor.putBoolean("radio5", false).apply();
            } else if (checkedId == R.id.rb_msg3){
                str_rbText = rb3.getText().toString();
                editText.setVisibility(View.INVISIBLE);
                editText.setText("");
                editor.putBoolean("radio3", true).apply();
                editor.putBoolean("radio1", false).apply();
                editor.putBoolean("radio2", false).apply();
                editor.putBoolean("radio4", false).apply();
                editor.putBoolean("radio5", false).apply();
            } else if (checkedId == R.id.rb_msg4){
                str_rbText = rb4.getText().toString();
                editText.setVisibility(View.INVISIBLE);
                editText.setText("");
                editor.putBoolean("radio4", true).apply();
                editor.putBoolean("radio1", false).apply();
                editor.putBoolean("radio2", false).apply();
                editor.putBoolean("radio3", false).apply();
                editor.putBoolean("radio5", false).apply();
            } else if (checkedId == R.id.rb_customMsg){
                editText.setVisibility(View.VISIBLE);
                editText.setHint("Enter your custom message");
                //if(editText.length() == 0){
                //  Toast.makeText(getContext(), "VISIBLE", Toast.LENGTH_SHORT).show();
                //}
                str_rbText = editText.getEditableText().toString().trim();
                editor.putBoolean("radio5", true).apply();
                editor.putBoolean("radio1", false).apply();
                editor.putBoolean("radio2", false).apply();
                editor.putBoolean("radio3", false).apply();
                editor.putBoolean("radio4", false).apply();
                //editText.setBackgroundDrawable(null);  //to remove underline of EditText
            }
            editor.putString("msg2send", str_rbText).apply();
        }
    });
person Noman Arif    schedule 18.09.2017