Android系统主要提供了3种方式用于简单地实现数据持久化功能,即文件存储、SharedPreferences存储和数据库存储。
文件存储
默认位置在/data/data/<package name>/files/
public class MainActivity extends AppCompatActivity { private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.edit); } @Override protected void onDestroy() { super.onDestroy(); String inputText = edit.getText().toString(); save(inputText); } public void save(String inputText) { FileOutputStream out ; BufferedWriter writer = null; try { out = openFileOutput("data",Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); }catch (IOException e) { e.printStackTrace(); } finally { try{ if(writer != null) { writer.close(); } }catch (IOException e) { e.printStackTrace(); } } } }
通过openFileOutput()得到一个FileOutputStream对象,再构建出一个OutputStreamWriter——BufferedWriter,通过BufferedWriter将文本内容写入到文件中。适合存储一些简单的文本数据或二进制数据。
从文件中读取数据
使用Context类中的openFileInput()方法。
SharedPreferences存储
实现记住密码功能
activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18dp" android:text="Account:" /> <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18sp" android:text="Password:" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/remember_pass" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="Remember password" /> </LinearLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity { private SharedPreferences pref; private SharedPreferences.Editor editor; private EditText accountEdit; private EditText passwordEdit; private Button login; private CheckBox rememberPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref = PreferenceManager.getDefaultSharedPreferences(this); accountEdit = (EditText)findViewById(R.id.account); passwordEdit = (EditText) findViewById(R.id.password); rememberPass = (CheckBox) findViewById(R.id.remember_pass); login = (Button)findViewById(R.id.login); boolean isRemember = pref.getBoolean("remember_password",false); if(isRemember) { String account = pref.getString("account",""); String password = pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); rememberPass.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if(account.equals("admin")&&password.equals("123456")) { editor = pref.edit(); if(rememberPass.isChecked()) { editor.putBoolean("remember_password",true); editor.putString("account",account); editor.putString("password",password); }else { editor.clear(); } editor.apply(); Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this,"account or password is invalid", Toast.LENGTH_SHORT).show(); } } }); } }
关键:得到SharedPreferences(三种方法,getSharedPreferences,getPreferences,getDefaultSharedPreferences),
调用SharedPreferences.edit()方法获得Editor对象,
添加数据(如editor.putString("name","Tom");
最后editor.apply()提交。
SQLite数据库存储
关键:
创建一个自己的帮助类继承SQLiteOpenHelpe,重写onCreate()和onUpdate()。
通过getReadableDatebase()和getWritableDatabase()创建或打开一个数据库。
对数据的四种操作方法:insert、update、delete、query。