java - RadioGroup 中 RadioButton 的 SharedPreferences.

标签 java android radio-button

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

 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 cat ,,,,我的代码错误


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

布局/////////////这是显示 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>

Activity 中

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;
                }
            }
        });

之后将 radio 值放入共享首选项中。 当您调用共享首选项值时,只需检查值并根据它启用单选按钮。

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

相关文章:

java - 在 Android Mapsforge map 上添加多个标记

java - ssl 地址的 URL 打开连接抛出断言异常

android - 在 Android Studio 中用 C++ 编写应用程序?

php - 解析错误 : expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in . .?

html - 在两个地方有相同的单选按钮

java - 在构造函数中泄漏它

java - 对于带有 anchor 的链接,HttpClient 出现 400 错误

java - Android 在内部类中动态更改按钮背景

Android - Magento REST Api 无法正确响应

flutter - 如何使 Radio Widget 看起来像一个按钮