如何对单选组中的单选按钮使用共享的首选项。我有两个按钮(下一个和上一个)。我想在用户转到下一个并再次回到上一个时检查用户选择的选项

 radioGroup = findViewById( R.id.radioGroup );
       radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               ToggleableRadioButton radioButton = (ToggleableRadioButton) group. findViewById(checkedId);
               int checkedIndex = group.indexOfChild(radioButton);
               SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
               SharedPreferences.Editor editor = sharedPreferences.edit();
               editor.putInt("check", checkedIndex);
               editor.apply();
           }
       } );




 private void Update(){
        SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        int savedRadioIndex = sharedPreferences.getInt("check", 0);
        System.out.println("savedRadioIndex is "+savedRadioIndex);
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
       ToggleableRadioButton savedCheckedRadioButton = (ToggleableRadioButton) radioGroup.getChildAt(savedRadioIndex);
        savedCheckedRadioButton.setChecked(true);//error on this line


    }


////// log我的代码错误


Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.quiz.ToggleableRadioButton.setChecked(boolean)' on a null object reference


布局/////////////这是layou,其中RadioGroup和单选按钮显示



<RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <com.example.quiz.ToggleableRadioButton
                android:id="@+id/option1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@android:color/white"
                android:elevation="4dp"
                android:padding="16dp"
                android:text="Option 1"
                android:textSize="20sp"

                />

            <com.example.quiz.ToggleableRadioButton
                android:id="@+id/option2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@android:color/white"
                android:elevation="4dp"
                android:padding="16dp"
                android:text="Option 2"
                android:textSize="20sp"

                />

            <com.example.quiz.ToggleableRadioButton
                android:id="@+id/option3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@android:color/white"
                android:elevation="4dp"
                android:padding="16dp"
                android:text="Option 3"
                android:textSize="20sp"

                />

            <com.example.quiz.ToggleableRadioButton
                android:id="@+id/option4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:elevation="4dp"
                android:padding="16dp"
                android:text="Option 4"
                android:textSize="20sp"

                />

        </RadioGroup>

最佳答案

试试这个

在布局文件中

<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       >

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton2" />
    </RadioGroup>


活动中

String radiovalue=null; //globally declared

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButton:
                       radiovalue= R1.getText().toString();//R1 is radiobutton1
                        break;
                    case R.id.radioButton2:
                         radiovalue= R2.getText().toString();//R2 is radiobutton2
                        break;
                }
            }
        });


之后,将radiovalue置于共享首选项中。
调用共享首选项值时,只需检查值并根据其启用单选按钮即可。

关于java - RadioGroup中RadioButton的SharedPreferences。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62378765/

10-12 06:18