Android 3种数据保存(SharedPreferences存储 内部文件存储 数据库存储)
第一步:在xml文件上界面布局
这边采用LinearLayour布局,添加3个按钮来实现界面的跳转,代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context=".MainActivity"> 8 <Button 9 android:id="@+id/b1" 10 android:layout_marginTop="50dp" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:text="内置存储"/> 14 15 <Button 16 android:id="@+id/b6" 17 android:layout_marginTop="50dp" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:textAllCaps="false" 21 android:text="SharedPreferences存储"/> 22 23 <Button 24 android:id="@+id/b7" 25 android:layout_marginTop="50dp" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:textAllCaps="false" 29 android:text="SQLite存储"/> 30 31</LinearLayout>
界面截图:

第二步:在Mainactivity.java完成功能的实现
找到ID并绑定
3个按钮的监听事件(界面的跳转)
1 b1.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 Intent intent=new Intent(MainActivity.this,Main2Activity.class); 9 startActivity(intent); 10 } 11 }); 12 b6.setOnClickListener(new View.OnClickListener() { 13 14 15 @Override 16 public void onClick(View view) { 17 18 19 Intent intent=new Intent(MainActivity.this,Main3Activity.class); 20 startActivity(intent); 21 } 22 }); 23 b7.setOnClickListener(new View.OnClickListener() { 24 25 26 @Override 27 public void onClick(View view) { 28 29 30 Intent intent=new Intent(MainActivity.this,Main4Activity.class); 31 startActivity(intent); 32 } 33 });
第三步:各功能的实现
①、内部文件存储
创建一个xml文件(activity_main2)和一个java文件(Main2Activity),来实现内部文件存储界面的布局和功能的实现。
第一:这边的布局采用LinearLayout布局,首先添加一个EditText控件来输入我们要保存的信息,在添加3个按钮的实现不同的功能,点击第一个按钮把当前输入的信息保存到手机内部文件夹上,第二个按钮打开当前文件夹显示保存的信息,第三个按钮删除手机内部文件夹下所保存的文件,代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".Main2Activity"> 9 <EditText 10 android:id="@+id/e1" 11 android:layout_marginTop="50dp" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:textSize="20dp" /> 15 <Button 16 android:id="@+id/b2" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="30dp" 20 android:text="保存信息"/> 21 <Button 22 android:id="@+id/b3" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:text="获取信息"/> 26 <Button 27 android:id="@+id/b4" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:text="删除文件"/> 31 <TextView 32 android:id="@+id/t1" 33 android:layout_width="match_parent" 34 android:textSize="30sp" 35 android:layout_marginTop="20dp" 36 android:text="" 37 android:gravity="center" 38 android:layout_height="wrap_content"/> 39 40</LinearLayout>
界面截图:

第二:内部文件存储功能的实现
- 获取到当前的id并绑定

- 3个按钮的监听事件
按钮1(b2)保存信息
1 b2.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 try { 9 10 11 12 } 13 catch (Exception e) 14 { 15 16 17 e.printStackTrace(); 18 } 19 } 20 21 }); 22 23 24 b2.setOnClickListener(new View.OnClickListener() { 25 26 27 @Override 28 public void onClick(View view) { 29 30 31 try { 32 33 34 OutputStream outputStream=openFileOutput("myfile",MODE_PRIVATE); 35 //MODE_PRIVATE,覆盖原有的文件;MODE_APPEND,内容追加到原来的文件上。 36 if(e1.getText().toString().equals("")) 37 { 38 39 40 Toast.makeText(Main2Activity.this,"当前为空,请输入信息",Toast.LENGTH_SHORT).show(); 41 return; 42 } 43 outputStream.write(e1.getText().toString().getBytes()); 44 outputStream.flush();; 45 outputStream.close(); 46 Toast.makeText(Main2Activity.this,"保存信息成功",Toast.LENGTH_SHORT).show(); 47 } 48 catch (Exception e) 49 { 50 51 52 e.printStackTrace(); 53 Toast.makeText(Main2Activity.this,"保存信息成功失败",Toast.LENGTH_SHORT).show(); 54 } 55 } 56 57 });
按钮2(b3)获取信息
1 b3.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 try { 9 10 11 12 } 13 catch (Exception e) 14 { 15 16 17 e.printStackTrace(); 18 } 19 } 20 }); 21 22 23 b3.setOnClickListener(new View.OnClickListener() { 24 25 26 @Override 27 public void onClick(View view) { 28 29 30 StringBuffer stringBuffer=new StringBuffer(); 31 try { 32 33 34 InputStream inputStream=openFileInput("myfile"); 35 BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); 36 String cout=null; 37 while((cout=bufferedReader.readLine())!=null) 38 { 39 40 41 stringBuffer.append(cout); 42 } 43 Toast.makeText(Main2Activity.this,"获取信息成功",Toast.LENGTH_SHORT).show(); 44 t1.setText(stringBuffer.toString()); 45 bufferedReader.close(); 46 } 47 catch (Exception e) 48 { 49 50 51 e.printStackTrace(); 52 Toast.makeText(Main2Activity.this,"获取信息失败",Toast.LENGTH_SHORT).show(); 53 } 54 } 55 });
按钮3(b4)删除文件
1 b4.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 boolean b=deleteFile("myfile"); 9 if(b==true) 10 { 11 12 13 Toast.makeText(getApplicationContext(), "删除文件成功", Toast.LENGTH_SHORT).show(); 14 } 15 else 16 { 17 18 19 Toast.makeText(getApplicationContext(), "删除文件失败", Toast.LENGTH_SHORT).show(); 20 } 21 } 22 });
界面截图:

②、SharedPreferences存储
创建一个xml文件(activity_main3)和一个java文件(Main3Activity),来实现内部文件存储界面的布局和功能的实现。
第一:这边的布局采用LinearLayout布局,首先添加两个EditText控件来输入账号和密码,在添加3个按钮的实现不同的功能,点击第一个按钮注册账号,第二个按钮重新输入信息,第三个按钮登陆账号,代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 android:gravity="center" 9 tools:context=".Main3Activity"> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal"> 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:padding="20dp" 18 android:textSize="20sp" 19 android:text="用户名:"/> 20 <EditText 21 android:id="@+id/username" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:hint="请输入账号" 25 android:singleLine="true" /> 26 </LinearLayout> 27 <LinearLayout 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:padding="20dp" 31 android:orientation="horizontal"> 32 <TextView 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:padding="20dp" 36 android:textSize="20sp" 37 android:text="密码:"/> 38 <EditText 39 android:id="@+id/password" 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" 42 android:hint="请输入密码" 43 android:inputType="textPassword" 44 android:singleLine="true" /> 45 </LinearLayout> 46 47<LinearLayout 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:gravity="center" 51 android:orientation="horizontal"> 52 <Button 53 android:id="@+id/login" 54 android:layout_width="0dp" 55 android:layout_weight="1" 56 android:layout_gravity="center" 57 android:layout_height="wrap_content" 58 android:text="注册"/> 59 <Button 60 android:id="@+id/delect" 61 android:layout_width="0dp" 62 android:layout_weight="1" 63 android:layout_height="wrap_content" 64 android:layout_marginLeft="20dp" 65 android:text="重新输入"/> 66 67</LinearLayout> 68 <Button 69 android:id="@+id/see" 70 android:layout_width="100dp" 71 android:layout_marginTop="20dp" 72 android:layout_height="wrap_content" 73 android:text="登陆"/> 74</LinearLayout>
界面截图:

第二:SharedPreferences存储功能的实现
-
获取到当前的id并绑定

-
定义一个SharedPreferences

private SharedPreferences sharedPreferences; -
3个按钮的监听事件
按钮1(login)注册账号
1 login.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 if(username.getText().toString().equals("")||password.getText().toString().equals("")) 9 { 10 11 12 Toast.makeText(Main3Activity.this,"不能为空!",Toast.LENGTH_SHORT).show(); 13 return; 14 } 15 sharedPreferences=getSharedPreferences("data",Context.MODE_PRIVATE); 16 SharedPreferences.Editor editor=sharedPreferences.edit(); 17 editor.putString("username",username.getText().toString()); 18 editor.putString("password",password.getText().toString()); 19 editor.apply(); 20 Toast.makeText(Main3Activity.this,"注册成功!",Toast.LENGTH_SHORT).show(); 21 } 22 });
按钮2(delect)删除文件,重新输入
1 delect.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 SharedPreferences.Editor editor=sharedPreferences.edit(); 9 editor.clear(); 10 editor.apply(); 11 username.setText(""); 12 password.setText(""); 13 Toast.makeText(Main3Activity.this,"重新输入",Toast.LENGTH_SHORT).show(); 14 } 15 });
按钮3(see)跳转到登陆账号界面
创建一个xml文件(activity_main5)和一个java文件(Main5Activity),来实现登陆账号功能
第一:这边的布局采用LinearLayout布局,首先添加两个EditText控件来输入账号和密码,在添加1个按钮的实现账号登陆,代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 android:gravity="center" 9 tools:context=".Main5Activity"> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:padding="20dp" 14 android:orientation="horizontal"> 15 <TextView 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:textSize="20sp" 19 android:text="账号:"/> 20 <EditText 21 android:id="@+id/zh1" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:textSize="20sp" 25 android:text=""/> 26 </LinearLayout> 27 <LinearLayout 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:padding="20dp" 31 android:orientation="horizontal"> 32 <TextView 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:textSize="20sp" 36 android:text="密码:"/> 37 <EditText 38 android:id="@+id/mm1" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" 41 android:textSize="20sp" 42 android:text=""/> 43 </LinearLayout> 44 <Button 45 android:id="@+id/success" 46 android:layout_width="100dp" 47 android:layout_height="wrap_content" 48 android:text="确认"/> 49</LinearLayout>
界面截图:

第二:功能实现(Main5Activity)
获取到当前的id并绑定

获取注册过的账号和密码

按钮的监听事件(判断账号密码是否为空和账号密码是否存在)
1success.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 if(zh1.getText().toString().equals("")||mm1.getText().toString().equals("")) 9 { 10 11 12 Toast.makeText(Main5Activity.this,"不能为空!",Toast.LENGTH_SHORT).show(); 13 return; 14 } 15 else if(zh1.getText().toString().equals(userzh)&&mm1.getText().toString().equals(usermm)) 16 { 17 18 19 Toast.makeText(Main5Activity.this,"登陆成功",Toast.LENGTH_SHORT).show(); 20 } 21 else 22 { 23 24 25 Toast.makeText(Main5Activity.this,"账号或密码错误",Toast.LENGTH_SHORT).show(); 26 } 27 28 } 29 });
界面截图:

③、SQLite存储
创建一个xml文件(activity_main4)和一个java文件(Main4Activity),来实现SQLite存储界面的布局和功能的实现。
第一:这边的布局采用LinearLayout布局,首先添加两个EditText控件来输入账号和密码,在添加2个按钮的实现不同的功能,点击第一个按钮注册账号,第二个按钮重新输入,之后在定义两个TextView来显示注册的账号密码,代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 android:gravity="center" 9 tools:context=".Main4Activity"> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal"> 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:padding="20dp" 18 android:textSize="20sp" 19 android:text="用户名:"/> 20 <EditText 21 android:id="@+id/usernames" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:hint="请输入账号" 25 android:singleLine="true" /> 26 </LinearLayout> 27 <LinearLayout 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:padding="20dp" 31 android:orientation="horizontal"> 32 <TextView 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:padding="20dp" 36 android:textSize="20sp" 37 android:text="密码:"/> 38 <EditText 39 android:id="@+id/passwords" 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" 42 android:hint="请输入密码" 43 android:inputType="textPassword" 44 android:singleLine="true" /> 45 </LinearLayout> 46 47 <LinearLayout 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:gravity="center" 51 android:orientation="horizontal"> 52 <Button 53 android:id="@+id/register" 54 android:layout_width="100dp" 55 android:layout_gravity="center" 56 android:layout_height="wrap_content" 57 android:text="注册"/> 58 <Button 59 android:id="@+id/again" 60 android:layout_width="100dp" 61 android:layout_height="wrap_content" 62 android:layout_marginLeft="20dp" 63 android:text="重新输入"/> 64 </LinearLayout> 65 <LinearLayout 66 android:layout_width="match_parent" 67 android:layout_height="wrap_content" 68 android:gravity="center" 69 android:layout_marginTop="50dp" 70 android:orientation="horizontal"> 71 72 <TextView 73 android:layout_width="wrap_content" 74 android:layout_height="wrap_content" 75 android:textSize="20sp" 76 android:text="账号:"/> 77 <TextView 78 android:id="@+id/zh" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:textSize="20sp" 82 android:text=""/> 83 <TextView 84 85 android:layout_width="wrap_content" 86 android:layout_height="wrap_content" 87 android:layout_marginLeft="50dp" 88 android:textSize="20sp" 89 android:text="密码:"/> 90 <TextView 91 android:id="@+id/mm" 92 android:layout_width="wrap_content" 93 android:layout_height="wrap_content" 94 android:textSize="20sp" 95 android:text=""/> 96 </LinearLayout> 97 98</LinearLayout>
界面截图:

第二:创建数据库文件(MyDateBase)
1public class MyDateBase { 2 3 4 5}
继承SQLiteOpenHelper
1public class MyDateBase extends SQLiteOpenHelper { 2 3 4 5}
鼠标移至SQLiteOpenHelper按下Alt+Enter,选择第一个,之后按下ok


鼠标移至SQLiteOpenHelper按下Alt+Enter,选择第一个,之后按下ok

创建一个命名为first的数据表(id,username,password)
private String createAppTable="create table first(id integer primary key autoincrement,username varchar(20),password varchar(20))";
在onCreate函数中执行SQL语句
1sqLiteDatabase.execSQL(createAppTable); 2sqLiteDatabase.execSQL("insert into first(id,username,password) values(?,?,?)",new String[]{ 3 4 "1","",""}); 5System.out.println("创建数据库成功");
根据Id找到账号和密码并更新账号和密码,在倒数第二个花括号添加类
1 public String getusernameById(SQLiteDatabase db,String id)//根据Id找到账号 2 { 3 4 5 Cursor cursor=db.rawQuery("select * from first where id=?",new String[]{ 6 7 id}); 8 if(cursor.moveToNext()) 9 { 10 11 12 return cursor.getString(cursor.getColumnIndex("username")); 13 } 14 return ""; 15 } 16 public void updateusername(SQLiteDatabase db,String id,String username)//更新账号 17 { 18 19 20 db.execSQL("update first set username=? where id=?",new String[]{ 21 22 username,id}); 23 } 24 25 public String getpasswordById(SQLiteDatabase db,String id)//根据Id找到密码 26 { 27 28 29 Cursor cursor=db.rawQuery("select * from first where id=?",new String[]{ 30 31 id}); 32 if(cursor.moveToNext()) 33 { 34 35 36 return cursor.getString(cursor.getColumnIndex("password")); 37 } 38 return ""; 39 } 40 public void updatepassword(SQLiteDatabase db,String id,String password)//更新密码 41 { 42 43 44 db.execSQL("update first set password=? where id=?",new String[]{ 45 46 password,id}); 47 } 48 public void delectdb(SQLiteDatabase db,String id)//删除数据表中的信息 49 { 50 51 52 db.execSQL("delete from first where id=?",new String[]{ 53 54 id}); 55 }
第三:返回Main4Activity文件,实现功能的实现
获取到当前的id并绑定

并添加SQLiteDatabase和MyDateBase

访问数据表

更新用户名和密码
1public void UpdateUserName()//更新账号 2 { 3 4 5 myDateBase.updateusername(db,"1",usernames.getText().toString()); 6 } 7public void UpdatePassWord()//更新密码 8 { 9 10 11 myDateBase.updatepassword(db,"1",passwords.getText().toString()); 12 }
按钮的监听事件
1 register.setOnClickListener(new View.OnClickListener() { 2 3 4 @Override 5 public void onClick(View view) { 6 7 8 if(passwords.getText().toString().equals("")||usernames.getText().toString().equals("")) 9 { 10 11 12 Toast.makeText(Main4Activity.this,"注册失败,不能为空!",Toast.LENGTH_SHORT).show(); 13 return; 14 } 15 UpdateUserName(); 16 UpdatePassWord(); 17 Toast.makeText(Main4Activity.this,"注册成功!",Toast.LENGTH_SHORT).show(); 18 zh.setText(usernames.getText().toString()); 19 mm.setText(passwords.getText().toString()); 20 } 21 }); 22 again.setOnClickListener(new View.OnClickListener() { 23 24 25 @Override 26 public void onClick(View view) { 27 28 29 usernames.setText(""); 30 passwords.setText(""); 31 } 32 });
获取账号和密码并显示
1 public void getUserName() { 2 3 //获取用户名 4 String user=myDateBase.getusernameById(db, "1"); 5 zh.setText(user); 6 } 7 public void getPassword() { 8 9 //获取密码 10 String pass=myDateBase.getpasswordById(db, "1"); 11 mm.setText(pass); 12 }

删除数据表的信息
1public void DelectDb() 2 { 3 4 5 myDateBase.delectdb(db,"1"); 6 }
本项目的源代码链接: