初学andrioid的文件存储需要了解SharedPreferences的概念
SharedPreferences简介
SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
SharedPreferences实例:
代码如下:
login:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="@dimen/activity_horizontal_margin" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/logintop" > <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:drawableLeft="@drawable/icon_user" android:hint="@string/et" android:drawablePadding="10dp" android:background="@android:drawable/edit_text" /> <requestFocus/> <EditText android:id="@+id/etpass" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:layout_below="@id/et" android:drawableLeft="@drawable/icon_pass" android:hint="@string/etpass" android:background="@android:drawable/edit_text" android:drawablePadding="10dp" /> <requestFocus/> <LinearLayout android:layout_below="@id/etpass" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/radioButton2" android:layout_weight="1" /> <Button android:layout_marginLeft="10dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="denglu" android:background="@drawable/btn_select" android:hint="@string/butreit" /> </LinearLayout> </RelativeLayout>
main:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/login_bg" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="bzu.edn.cn.login.MainActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>activity:
package bzu.edn.cn.login; import android.content.Context; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; public class MainActivity extends AppCompatActivity { private EditText editText1,editText2; private SharedPreferences sharedPreferences; private RadioButton radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1= (EditText) findViewById(R.id.et); editText2= (EditText) findViewById(R.id.etpass); radioButton= (RadioButton) findViewById(R.id.radioButton2); sharedPreferences=getSharedPreferences("data",Context.MODE_PRIVATE); boolean ss=sharedPreferences.getBoolean("data",true); if(ss){ String name=sharedPreferences.getString("name",""); String pass=sharedPreferences.getString("pass",""); editText1.setText(name); editText2.setText(pass); radioButton.setChecked(true); } } public void denglu(View view){ String name=editText1.getText().toString(); String pass=editText2.getText().toString(); if("ass".equals(name)&&"123456".equals(pass)){ SharedPreferences.Editor editor=sharedPreferences.edit(); if (radioButton.isChecked()){ editor.putBoolean("data",true); editor.putString("name",name); editor.putString("pass",pass); }else { editor.clear(); }editor.commit(); } } }对于Activity里关于SharedPreferences get put 等简单解释一下:
1、使用SharedPreferences保存数据方法如下: //实例化SharedPreferences对象(第一步) SharedPreferences mySharedPreferences= getSharedPreferences("test",
Context.MODE_PRIVATE
);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString("name", "name");
editor.putString("pass", "pass");
//提交当前数据
editor.commit();
Context.MODE_PRIVATE
);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
String pass =sharedPreferences.getString("pass", "");
//使用toast信息提示框显示信息