简体   繁体   English

如何在共享偏好中使用无线电组?

[英]How to use radio group in shared preference?

I'm doing my mobile project, I'm using shared preference to save user's clicks for later usage. 我正在做我的移动项目,我正在使用共享首选项来保存用户的点击以供以后使用。 One of them is radio buttons, after some research I found that to get the specific radio button that was clicked, I have to use radio Group. 其中一个是单选按钮,经过一些研究,我发现要获得点击的特定单选按钮,我必须使用无线电组。 When I tried that, it only enables me to click on one radio button the other one when I click o it, the app is forced to stop. 当我尝试这个时,它只允许我点击一个单选按钮,当我点击它时,应用程序被迫停止。 I have created a class called Preference.java to set and get all of user clicks, including radio buttons. 我创建了一个名为Preference.java的类来设置和获取所有用户点击,包括单选按钮。

MainActivity.java 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 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 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 

请将它从布局文件中删除并尝试它似乎没有实现。

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

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