全局變量:在onDestory()
保存看到,如果一個SharedPreference變量爲空或不是
SharedPreferences prefs;
SharedPreferences.Editor editor;
:
editor.putInt("InfType", rgTypeOfInf.getCheckedRadioButtonId()); //saving value
被稱爲中onCreate()
:
rgTypeOfInf = (RadioGroup) mFrame2.findViewById(R.id.rgType);
rgTypeOfInf.check(prefs.getInt("InfType", 2131230725)); //retrieving the value
我如何檢查時我的應用程序加載以查看保存的變量是否存在或者是否爲null
。如果存在,請檢查radiogroup的單選按鈕,否則如果它是null
,默認情況下檢查radiogroup [0]位置?
我的XML文件:
<RadioGroup
android:id="@+id/rgType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="@+id/rbBridge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Bridge" />
<RadioButton
android:id="@+id/rbTunnel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tunnel" />
<RadioButton
android:id="@+id/rbHighway"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Highway" />
</RadioGroup>
['SharedPreferences#含有(String鍵)'](http://developer.android.com/reference/android/content/SharedPreferences.html#contains%28java.lang.String%29 )? – zapl