Android Spinner + SharedPreferences 保存上一次选择的值
android下拉可选菜单Spinner创建:
1, 创建一个HelloSpinner的android project
2, 修改main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/planet_prompt" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/planet_prompt" /> </LinearLayout> 3, 修改strings.xml文件,添加: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="planet_prompt">Choose a planet</string> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array> </resources> 4, 修改HelloSpinner.java文件中的OnCreate方法 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } 5, 创建一个内部类: public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(parent.getContext(), "The planet is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView parent) { // Do nothing. } } 6,设置监听器: spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
7, 运行结果如下:
8, 如果用户选择了spinner其中的某一个item, 跳入到另外一个activity中,当从这么activity跳回来的时候,如何让spinner记得用户之前选择的那个一个item呢?这里用到了SharedPreferences:
8.1 声明一个SharedPreferences: SharedPreferences settings;
Editor editorsettings;
settings = getSharedPreferences("preferences_settings",Context.MODE_PRIVATE);
editorsettings = settings.edit(); 其中“preferences_settings”就是这个SharedPreferences的标志,我觉得就像是唯一的名字一样。
8.2在spinner的setOnItemSelectedListener方法里保存当时用户选择的item的位置和值 spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int postion, long id) {
selectedName = parent.getItemAtPosition(postion).toString();
editorsettings.putInt("SelectedPosition", postion);
editorsettings.putString("SelectedName", selectedName);
editorsettings.commit();
8.3 读取存在SharedPreferences里的值 SharedPreferences settings = getSharedPreferences("preferences_settings",0); //与上面的保持一致
String name = settings.getString("SelectedName", "");
int position = settings.getInt("SelectedPosition", 0 );
spinner.setSelection(position); //设置spinner的值,让其当前选择项是与以前的一样。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。