我的 Android 之旅(十九)---数据存储--向SD卡中以实体类的方式存取数据
首先先新建一个实体类:
package com.jerehedu.jreduch04.entity; import java.io.Serializable; /** * Created by on 0162016/8/16. */ public class News implements Serializable{ private Boolean listShow; private String size; private Boolean notice; private Boolean components; private Boolean read; private Boolean replay; public News() { } public News(Boolean listShow, String size, Boolean notice, Boolean components, Boolean read, Boolean replay) { this.listShow = listShow; this.size = size; this.notice = notice; this.components = components; this.read = read; this.replay = replay; } public Boolean getListShow() { return listShow; } public void setListShow(Boolean listShow) { this.listShow = listShow; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public Boolean getNotice() { return notice; } public void setNotice(Boolean notice) { this.notice = notice; } public Boolean getComponents() { return components; } public void setComponents(Boolean components) { this.components = components; } public Boolean getRead() { return read; } public void setRead(Boolean read) { this.read = read; } public Boolean getReplay() { return replay; } public void setReplay(Boolean replay) { this.replay = replay; } }
对于该类来说,需要序列化推荐使用Serializable
实现序列化的方法
Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的),一是实现Parcelable接口(是Android特有功能,效率比实现Serializable接口高效,可用于Intent数据传递,也可以用于进程间通信(IPC))。实现Serializable接口非常简单,声明一下就可以了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。
在主函数中,首先先从实体类里获取数据,利用get方法获得原有的数据,
若存储在SD卡中,首先判断SD卡的状态,然后获取SD卡的根目录
.xml文件中的代码:
package com.jerehedu.jreduch04; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.jerehedu.jreduch04.entity.News; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.StreamCorruptedException; public class Save2Activity extends AppCompatActivity { private Button finish; private CheckBox c1,c2,c3,c4,c5; private TextView textSize; private LinearLayout ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_srcoll__view_to_card); finish= (Button) findViewById(R.id.finish); c1= (CheckBox) findViewById(R.id.c1); c2= (CheckBox) findViewById(R.id.c2); c3= (CheckBox) findViewById(R.id.c3); c4= (CheckBox) findViewById(R.id.c4); c5= (CheckBox) findViewById(R.id.c5); ll= (LinearLayout) findViewById(R.id.ll); textSize= (TextView) findViewById(R.id.textSize); readSetting(); //设置保存文件 ll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(Save2Activity.this); builder.setTitle("请选择"); builder.setIcon(R.mipmap.ic_launcher); final String[] items={"大","中","小"}; int i; for ( i=0;i<items.length;i++){ if (items[i].equals(textSize.getText())){ break; } } builder.setSingleChoiceItems(items,i , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { Toast.makeText(getBaseContext(), items[i].toString(), Toast.LENGTH_SHORT).show(); textSize.setText(items[i].toString()); dialog.dismiss(); } }); AlertDialog alertDialog=builder.create(); alertDialog.show(); } }); finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { News news = new News(); news.setListShow(c1.isChecked()); news.setSize(textSize.getText().toString()); news.setNotice(c2.isChecked()); news.setComponents(c3.isChecked()); news.setRead(c4.isChecked()); news.setReplay(c5.isChecked()); String state = Environment.getExternalStorageState(); if (!state.equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(Save2Activity.this, "SD卡未就绪", Toast.LENGTH_SHORT).show(); return; } File root = Environment.getExternalStorageDirectory(); FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(root + "/settings.txt"); oos = new ObjectOutputStream(fos); oos.writeObject(news); Toast.makeText(Save2Activity.this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); } public void readSetting(){ File root=Environment.getExternalStorageDirectory(); FileInputStream fis=null; ObjectInputStream ois=null; try { fis=new FileInputStream(root+"/settings.txt"); ois=new ObjectInputStream(fis); News news= (News) ois.readObject(); textSize.setText(news.getSize()); c1.setChecked(news.getListShow()); c2.setChecked(news.getComponents()); c3.setChecked(news.getNotice()); c4.setChecked(news.getRead()); c5.setChecked(news.getReplay()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { if (ois!=null){ try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jerehedu.jreduch04.ScrollView_VerticalActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:id="@+id/sv"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/p1" android:id="@+id/iv1"/> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="设置" android:gravity="center" android:textSize="25sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="完成" android:textSize="20sp" android:id="@+id/finish" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表显示摘要" android:textSize="24sp" android:gravity="center" android:id="@+id/listShow" android:layout_weight="1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="150dp" android:layout_gravity="center" android:id="@+id/c1" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:orientation="horizontal" android:id="@+id/ll" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="字体大小" android:textSize="24sp" android:id="@+id/size" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:id="@+id/textSize" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表页评论" android:textSize="24sp" android:gravity="center" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不限" android:textSize="20sp" android:layout_marginLeft="160dp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2G/3G网络流量" android:textSize="24sp" android:gravity="center" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控制流量" android:textSize="20sp" android:layout_marginLeft="100dp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清理缓存" android:textSize="24sp" android:gravity="center" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前缓存2.4MB" android:textSize="20sp" android:layout_marginLeft="100dp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="推送通知" android:textSize="24sp" android:gravity="center" android:id="@+id/notice" android:layout_weight="1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="200dp" android:layout_gravity="center" android:id="@+id/c2" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="互动插件" android:textSize="24sp" android:gravity="center" android:id="@+id/components" android:layout_weight="1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="200dp" android:layout_gravity="center" android:id="@+id/c3" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动优化阅读" android:textSize="24sp" android:gravity="center" android:id="@+id/read" android:layout_weight="1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="150dp" android:layout_gravity="center" android:id="@+id/c4" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"> </View> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收藏时转发" android:textSize="24sp" android:gravity="center" android:id="@+id/replay" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="175dp" android:layout_gravity="center" android:id="@+id/c5" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
运行结果如下:
在该布局中可以对单选框进行操作,也可以更改字体大小:
选择完成后点击保存按钮,实现数据的保存:
当退出该程序后重新进入后,仍然保持当时所选的界面:
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。