簡體   English   中英

SharedPreferences.for RadioGroup 中的 RadioButton

[英]SharedPreferences.for RadioButton in RadioGroup

如何在單選組中使用單選按鈕的共享首選項。 我有兩個按鈕(下一個和上一個)。 當用戶轉到下一個並再次回到上一個時,我想檢查用戶選擇的選項

 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


    }

/////記錄我的代碼的cat,,,,error


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>

活動中

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

之后將無線電值放入共享偏好中。 當您調用共享首選項值時,只需檢查值並根據它啟用單選按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM