0

5つのRadioButtonを持つRadioGroupを含むフラグメントアクティビティとButton.RadioButton5にEditTextがあります。RadioGroupの選択されたRadioButton状態は、フラグメントを使用するSharedPreferencesに保存されます。

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 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の状態が正しく保存されない活動を開始するにSharedPreferencesと負荷に保存してください。 1回目のRadioButtonの状態は、このアクティビティを呼び出すときに常にtrueになります。

答えて

0

選択したradioButtonの1つの状態を設定すると、他のradioButtonにfalseが設定されます。

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(); 
     } 
    }); 
関連する問題