SharedPreferences存储
1通过getSharedPreferences获得 SharedPreferences对象
2通过edit()方法获取Editor对象,
3通过Edtitor对象的putString()方法进行存储
4通过Editor对象的commit()方法进行提交
从存储中取出方法为String name=sharePreferences.getString()取出
Main
1 package demo01.example.administrator.demo01; 2 3 import android.content.SharedPreferences; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.CheckBox; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 import static demo01.example.administrator.demo01.R.id.user_et; 12 13 public class MainActivity extends AppCompatActivity { 14 EditText userName,passWord; 15 SharedPreferences sharedPreferences; 16 SharedPreferences.Editor editor; 17 CheckBox checkBox; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 userName= (EditText) findViewById(R.id.user_et); 23 passWord= (EditText) findViewById(R.id.pass_et); 24 checkBox= (CheckBox) findViewById(R.id.checkBox); 25 sharedPreferences=getSharedPreferences("MyInfor",MODE_PRIVATE); 26 editor=sharedPreferences.edit(); 27 String name=sharedPreferences.getString("useName",""); 28 if (name==null){ 29 checkBox.setChecked(false); 30 31 }else { 32 // checkBox.setChecked(true); 33 userName.setText(name); 34 } 35 } 36 public void doClick(View view){ 37 switch (view.getId()){ 38 39 case R.id.login: 40 String name=userName.getText().toString().trim(); 41 String password=passWord.getText().toString().trim(); 42 if(name.equals("admin")&&password.equals("123456")){ 44 editor.putString("useName",name); 45 46 editor.commit(); 47 Toast.makeText(MainActivity.this,"成功登录",Toast.LENGTH_SHORT).show(); 48 }else { 49 editor.remove("useName"); 50 editor.commit(); 51 } 52 break; 58 59 } 60 }
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" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content"> 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:id="@+id/user" 13 android:text="用户名" 14 android:textSize="40sp"/> 15 <EditText 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:id="@+id/user_et" 19 android:layout_weight="1" 20 /> 21 </LinearLayout> 22 <LinearLayout 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content"> 25 <TextView 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:id="@+id/password" 29 android:text="密码" 30 android:textSize="40sp"/> 31 <EditText 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:id="@+id/pass_et" 35 android:layout_weight="1"/> 36 </LinearLayout> 37 <CheckBox 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:text="保存用户名"/> 41 <LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content"> 44 <Button 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:text="登录" 48 android:onClick="doClick" 49 android:id="@+id/login"/> 50 <Button 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:text="取消" 54 android:id="@+id/cancel" 55 /> 56 </LinearLayout> 57 </LinearLayout>