SharedPreferences是Android平台上一个轻量级的存储接口,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此Activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出.
下面是一个保存登陆用户名的例子:
MyActivity:
package com.lee.SharedPreferenceStudy; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MyActivity extends Activity { /** * Called when the activity is first created. */ private EditText editText1,editText2; private CheckBox checkBox; private SharedPreferences preferences; private SharedPreferences.Editor editor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText1 = (EditText) findViewById(R.id.userName_et); editText2 = (EditText) findViewById(R.id.password_et); checkBox = (CheckBox) findViewById(R.id.save_cb); //1.获得SharedPreferences的对象 preferences =getSharedPreferences("UserInfo", MODE_PRIVATE); editor =preferences.edit(); //2.取出数据 String name =preferences.getString("username",""); if (name==null){ checkBox.setChecked(false); editor.remove("username"); editor.commit(); }else { checkBox.setChecked(true); editText1.setText(name); } } public void Sure(View view){ switch (view.getId()){ case R.id.ok_btn: String username =editText1.getText().toString().trim(); String password =editText2.getText().toString().trim(); if (username.equals("admin")&&password.equals("123")){ if (checkBox.isChecked()){ //3.存储数据 editor.putString("username",username); //4.提交数据 editor.commit(); }else { editor.remove("username"); editor.commit(); } Toast.makeText(MyActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MyActivity.this,"密码错误",Toast.LENGTH_SHORT).show(); } break; } } }
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <!--android:ems = "10" 设置TextView或者Edittext的宽度为10个字符的宽度。 8 当设置该属性后,控件显示的长度就为10个字符的长度,超出的部分将不显示。--> 9 <LinearLayout 10 android:orientation="horizontal" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_gravity="center_horizontal"> 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="用户名:" 18 android:layout_margin="10dp" 19 android:textSize="34sp" 20 android:id="@+id/userName_tv"/> 21 <EditText 22 android:layout_width="wrap_content" 23 android:layout_height="match_parent" 24 android:layout_margin="10dp" 25 android:textSize="34sp" 26 android:ems="10" 27 android:textColor="#333333" 28 android:id="@+id/userName_et"/> 29 </LinearLayout> 30 <LinearLayout 31 android:orientation="horizontal" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content"> 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="密 码:" 38 android:layout_margin="10dp" 39 android:textSize="34sp" 40 android:id="@+id/password_tv"/> 41 <EditText 42 android:layout_width="wrap_content" 43 android:layout_height="match_parent" 44 android:inputType="textPassword" 45 android:ems="10" 46 android:layout_margin="10dp" 47 android:textSize="34sp" 48 android:id="@+id/password_et"/> 49 </LinearLayout> 50 <LinearLayout 51 android:orientation="horizontal" 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content"> 54 <CheckBox android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:layout_margin="10dp" 57 android:checked="false" 58 android:id="@+id/save_cb" android:enabled="true"/> 59 <TextView 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:text="保存用户" 63 android:layout_margin="10dp" 64 android:textSize="34sp" 65 android:id="@+id/save_tv"/> 66 67 68 </LinearLayout> 69 <LinearLayout android:layout_width="match_parent" 70 android:layout_height="wrap_content" 71 android:orientation="horizontal"> 72 <Button android:layout_width="wrap_content" 73 android:layout_height="wrap_content" 74 android:text="确定" 75 android:layout_weight="1" 76 android:textSize="34sp" 77 android:layout_margin="40dp" 78 android:onClick="Sure" 79 android:id="@+id/ok_btn" 80 /> 81 <Button android:layout_width="wrap_content" 82 android:layout_height="wrap_content" 83 android:text="取消" 84 android:layout_weight="1" 85 android:textSize="34sp" 86 android:layout_margin="40dp" 87 /> 88 </LinearLayout> 89 90 </LinearLayout>
效果图:
时间: 2024-10-25 22:30:45