在应用程序中有两个单选按钮,每个按钮都有自己的值,第一个单选按钮的值为"2〃;第二个单选按钮的值为"0";4〃;。现在,我成功地实现了共享首选项,以便通过单击单选按钮来保存值。但有一件事是缺失的,那就是如果我选择单选按钮1,如果我关闭应用程序并再次打开应用程序,则单选按钮1不会被选中。我也尝试添加radiobutton.setChecked
,但它崩溃了。下面是xml和java代码。
custom_dialog.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup" />
</ScrollView>
method0.xml
<resources>
<integer-array name="method0">
<item>2</item>
</integer-array>
</resources>
method1.xml
<resources>
<integer-array name="method1">
<item>4</item>
</integer-array>
</resources>
CustomDialog.java
public class CustomDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater=getActivity().getLayoutInflater();
final View view=layoutInflater.inflate(R.layout.custom_dialog,null);
RadioGroup radioGroup=view.findViewById(R.id.radioGroup);
final int[] num0=getResources().getIntArray(R.array.method0);
final int[] num1=getResources().getIntArray(R.array.method1);
RadioButton radioButton0=new RadioButton(getActivity());
RadioButton radioButton1=new RadioButton(getActivity());
radioButton0.setText("A");
radioButton0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int method=num0[0];
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("key", method);
editor.apply();
}
});
radioButton0.setPadding(0,50,0,50);
radioButton0.setTextSize(17);
radioButton0.setTypeface(null, Typeface.BOLD);
radioButton0.setChecked(Update("key")); //Here this line it is getting crashed
radioButton1.setText("B");
radioButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int method=num1[0];
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("key", method);
editor.apply();
}
});
radioButton1.setPadding(0,50,0,50);
radioButton1.setTextSize(17);
radioButton1.setTypeface(null, Typeface.BOLD);
radioGroup.addView(radioButton0);
radioGroup.addView(radioButton1);
builder.setView(view)
.setTitle("Select Any Methods")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
private boolean Update(String key){
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key,false); //Here this line it is getting crashed
}
}
MainActivity.java
btnMethod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog(); //this will open the dialog along with two radiobuttons
}
});
private void openDialog() {
CustomDialog customDialog=new CustomDialog();
customDialog.show(getSupportFragmentManager(),"Custom Dialog");
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nabil.myapplication, PID: 17062
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:326)
at com.nabil.myapplication.CustomDialog.Update(CustomDialog.java:295)
at com.nabil.myapplication.CustomDialog.onCreateDialog(CustomDialog.java:69)
我不太了解android代码,但是,如果我只是检查了你的错误,那么它就来了,因为Integer和boolean这两个类之间没有父子关系。
这是因为您在共享首选项中存储了一个整数,然后尝试检索布尔值。
我建议存储单选按钮的值,如下所示:
editor.putBoolean("radioButton0", radioButton0.isChecked());
...
editor.putBoolean("radioButton1", radioButton1.isChecked());
编辑:
而不是使用
editor.putInt("key", method);
用途:
editor.putBoolean("radioButton0", radioButton0.isChecked());
与
而不是使用
radioButton0.setChecked(Update("key"));
用途:
radioButton0.setChecked(Update("radioButton0"));
这应该像你希望的那样工作。