如何在共享首选项中使用广播组?

How to use radio group in shared preference?

我正在做我的移动项目,我正在使用共享首选项来保存用户的点击以供以后使用。其中之一是单选按钮,经过一些研究我发现要获得被点击的特定单选按钮,我必须使用单选组。当我尝试这样做时,它只能让我在单击一个单选按钮时单击另一个单选按钮,该应用程序被迫停止。我创建了一个名为 Preference.java 的 class 来设置和获取所有用户点击,包括单选按钮。

MainActivity.java

    public class MainActivity extends AppCompatActivity  {

    private Preferences preferences;
    final String KEY_SAVED_CAR_INDEX = "KEY_SAVED_CAR_INDEX";
    final String KEY_SAVED_MAN_INDEX = "KEY_SAVED_MAN_INDEX";
    Button clickbtn;
    CheckBox start_from_home;
    RadioButton carchecked, manchecked;
    RadioGroup radioGroup;
    Spinner spinner ;
    EditText ed1;
    int selectedPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = (EditText) findViewById(R.id.edittext1);

        spinner=(Spinner)findViewById(R.id.spinner_hours);
        selectedPosition= spinner.getSelectedItemPosition();

        clickbtn = (Button) findViewById(R.id.click_btn);
        clickbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                init();
                Intent intent1 = new Intent(getApplicationContext(), Main2Activity.class);
                startActivity(intent1);
            }

        });

        start_from_home =(CheckBox) findViewById(R.id.start_check);
        start_from_home.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(start_from_home.isChecked()) {
                    preferences = new Preferences(MainActivity.this);
                    preferences.setstartfromhome(b);
                }
            }
        });

        radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
//        radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
                int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);
                int radioButtonID = radioGroup.getCheckedRadioButtonId();

                preferences = new Preferences(MainActivity.this);

                if(radioButtonID == R.id.car_radio) {
                    preferences.settransportationchecked(KEY_SAVED_CAR_INDEX, checkedIndex);
                }
                else if (radioButtonID== R.id.man_radio)
                    preferences.settransportationchecked(KEY_SAVED_MAN_INDEX,checkedIndex);
}
        });

    }
private void init() {

        preferences = new Preferences(MainActivity.this);

        preferences.setcurrentlocation(ed1.getText().toString());
     preferences.setspinnerhours(selectedPosition);
    }


}

activity_main.xml

<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/edittext1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RadioButton
            android:id="@+id/car_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/click_btn"
            android:layout_marginTop="25dp"
            android:text="Car" />



        <RadioButton
            android:id="@+id/man_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Walking"
            android:onClick="onRadioButtonClicked"
            android:layout_below="@+id/car_radio"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="51dp" />
    </RadioGroup>

Preference.java

public void settransportationchecked(String key, int value){
    SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}
android:onClick="onRadioButtonClicked 

请将其从布局文件中删除并尝试,但似乎未实现。