我用 OnItemSelectedListener 函数做了一个微调器。我从一个包含 4 个选项的数组中创建它:
String[] arrayOfString = new String[4];
arrayOfString[0] = "Kiskunfélegyháza HQ [160kbps MP3]";
arrayOfString[1] = "Kiskunfélegyháza MQ [64kbps AAC]";
arrayOfString[2] = "Kiskunmajsa HQ [160kbps MP3]";
arrayOfString[3] = "Kiskunmajsa MQ [64kbps AAC]";
this.items = arrayOfString;
加载数据中:
PreferenceManager.setDefaultValues(this, 0x7f040000, false);
final SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.mWebView_enabled = localSharedPreferences.getBoolean("mWebView_enabled", true);
this.savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
this.savszelesseg_saved = this.savszelesseg_port;
final Spinner localSpinner = (Spinner)findViewById(R.id.savszelesseg);
ArrayAdapter localArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, this.items);
localArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(localArrayAdapter);
如果有人选择一个选项,它将被保存到 SharedPreferencies 并更改音乐。但是,当有人退出然后重新打开应用程序时,如何取回数据?
我将它保存为 savszelesseg_mentes,并保存 savszelesseg_port。我还有一个选项菜单来保存此设置。
localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View paramView, int paramInt, long paramLong)
{
if (!savszelesseg_port_modositva)
{
savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
savszelesseg_port_modositva = true;
}
savszelesseg_beallitas = items[paramInt];
if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]")
{
savszelesseg_port = "8500/relay_911";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunfélegyháza MQ [64kbps AAC]")
{
savszelesseg_port = "8500/relay_mobile_911";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunmajsa HQ [160kbps MP3]")
{
savszelesseg_port = "8500/relay_882";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (savszelesseg_beallitas == "Kiskunmajsa MQ [64kbps AAC]")
{
savszelesseg_port = "8500/relay_mobile_882";
SharedPreferences.Editor editor = localSharedPreferences.edit();
editor.putString("savszelesseg_mentes", savszelesseg_port);
editor.commit();
}
if (lejatszas == 1 && savszelesseg_port_modositva)
{
stopPlaying();
startPlaying();
}
if (lejatszas == 0 && savszelesseg_port_modositva == true)
{
startPlaying();
}
}
如果没有选择:
public void onNothingSelected(AdapterView parentView)
{
}