4
如何保存當前選定的微調器值,以便當我重新打開應用程序時默認會自動選擇保存的值?使用共享首選項保存微調器選定項目的值
我當前的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",spinner.getSelectedItem().toString());
String savedValue=spinner.getSelectedItem().toString();
for(int i=0;i<6;i++)
if(savedValue.equals(spinner.getItemAtPosition(i).toString()))
{
spinner.setSelection(i);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});
喜RASEL其實我也想知道如何使用微調請共享偏好u能解釋它請請.. –
請參閱更新的答案如何保存你的微調值共享偏好 – Rasel
vv謝謝拉塞爾幫助我...我正在實施它,如果我覺得有任何問題,我會問你..我認爲它應該現在完成... –