gpt4 book ai didi

java - RadioGroup 中 RadioButton 的 SharedPreferences.

转载 作者:行者123 更新时间:2023-12-01 16:16:27 25 4
gpt4 key购买 nike

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

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com