Android-如何将用户设置进行保存?(内部存储、SD卡,用类作为媒介)

    本次实例是将用户的设置信息,以实体类为媒介,保存到内部存储或者SD卡的文件里。再次打开应用时,程序会自动调用文件中实体类,并对设置信息进行设置,使其恢复到用户最终保存的状态。

首先,创建一个实体类(这里要实现 Serializable 接口,即序列化,这样才能让类在内存中传递):

public class SettingData implements Serializable{
    private boolean titleShow;
    private String textSize;
    private boolean notify;
    private boolean plug;
    private boolean autoRead;
    private boolean transmit;

    public SettingData() {
    }

    public SettingData(boolean titleShow, String textSize, boolean notify, boolean plug, boolean autoRead, boolean transmit) {
        this.titleShow = titleShow;
        this.textSize = textSize;
        this.notify = notify;
        this.plug = plug;
        this.autoRead = autoRead;
        this.transmit = transmit;
    }

    public boolean isTransmit() {
        return transmit;
    }

    public void setTransmit(boolean transmit) {
        this.transmit = transmit;
    }

    public boolean isTitleShow() {
        return titleShow;
    }

    public void setTitleShow(boolean titleShow) {
        this.titleShow = titleShow;
    }

    public String getTextSize() {
        return textSize;
    }

    public void setTextSize(String textSize) {
        this.textSize = textSize;
    }

    public boolean isNotify() {
        return notify;
    }

    public void setNotify(boolean notify) {
        this.notify = notify;
    }

    public boolean isPlug() {
        return plug;
    }

    public void setPlug(boolean plug) {
        this.plug = plug;
    }

    public boolean isAutoRead() {
        return autoRead;
    }

    public void setAutoRead(boolean autoRead) {
        this.autoRead = autoRead;
    }
}
接下里是Layout布局,这里楚略地弄了一个设置界面:

<?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"
    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="jerehdu.com.jerehdu04.Scroll1Activity"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="6dp">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/left_menu_activity_pressed"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginLeft="120dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="设置"
                        android:textSize="20sp"
                        android:layout_gravity="center"
                        android:layout_weight="1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="完成"
                        android:textSize="18sp"
                        android:id="@+id/setting"/>
                </LinearLayout>

            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:weightSum="1"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="显示列表摘要"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/titleShow"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:id="@+id/text"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="字体大小"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="中"
                    android:textSize="14sp"
                    android:id="@+id/textSize"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/left_menu_blgj_pressed"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="列表页评论"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="不限"
                    android:textSize="14sp"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/left_menu_blgj_pressed"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2G/3G网络流量"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="较省流量"
                    android:textSize="14sp"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/left_menu_blgj_pressed"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="清理缓存"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="当前缓存:5.21MB"
                    android:textSize="14sp"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/left_menu_blgj_pressed"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="推送通知"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/notify"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="互动插件"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/plug"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="自动优化阅读"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/autoRead"/>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eaeaea"
                android:layout_marginBottom="6dp"
                android:layout_marginTop="6dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="收藏时转发"
                    android:textSize="16sp"
                    android:layout_weight="1"/>
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/transmit"/>
            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>


最后,就是Activity啦,其功能实现为设置好后点击右上角“完成”,即可保存设置内容。

方法一:通过SharedPreferences

,保存设置内容:
public class Setting2Activity extends AppCompatActivity {

    private CheckBox titleShow,notify,plug,autoRead,transmit;
    private TextView setting,textSize;
    private LinearLayout text;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting2);
        titleShow = (CheckBox) findViewById(R.id.titleShow);
        notify = (CheckBox) findViewById(R.id.notify);
        plug = (CheckBox) findViewById(R.id.plug);
        autoRead = (CheckBox) findViewById(R.id.autoRead);
        transmit = (CheckBox) findViewById(R.id.transmit);
        setting = (TextView) findViewById(R.id.setting);
        textSize = (TextView) findViewById(R.id.textSize);
        text = (LinearLayout) findViewById(R.id.text);

        //开启设置以保存的设置
        settingStart();

        //监听字体大小的改变,选择方式:对话框
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Setting2Activity.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 which) {
                        Toast.makeText(getBaseContext(),items[which].toString(),Toast.LENGTH_SHORT).show();
                        textSize.setText(items[which].toString());
                        dialog.dismiss();
                    }
                });
                builder.setCancelable(true);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });

        sp = getSharedPreferences("mysetting.txt", Context.MODE_PRIVATE);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //存入数据
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("titleShow",titleShow.isChecked());
                editor.putString("textSize",textSize.getText().toString());
                editor.putBoolean("notify",notify.isChecked());
                editor.putBoolean("plug",plug.isChecked());
                editor.putBoolean("autoRead",autoRead.isChecked());
                editor.putBoolean("transmit",transmit.isChecked());
                editor.commit();
                Toast.makeText(getBaseContext(),"保存成功",Toast.LENGTH_SHORT).show();
            }
        });

    }

    //启动函数
    public void settingStart(){
        sp = getSharedPreferences("mysetting.txt", Context.MODE_PRIVATE);
        if(sp.getString("textSize","").equals("")){
            Toast.makeText(getBaseContext(),"还没有预设值",Toast.LENGTH_SHORT).show();
            return;
        }
        titleShow.setChecked(sp.getBoolean("titleShow",false));
        textSize.setText(sp.getString("textSize",""));
        notify.setChecked(sp.getBoolean("notify",false));
        plug.setChecked(sp.getBoolean("plug",false));
        autoRead.setChecked(sp.getBoolean("autoRead",false));
        transmit.setChecked(sp.getBoolean("transmit",false));
    }
}

方法二:实体类保存,并将其传到SD卡文件中:

public class SettingActivity extends AppCompatActivity {

    private CheckBox titleShow,notify,plug,autoRead,transmit;
    private TextView setting,textSize;
    private LinearLayout text;
    private SettingData settingData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll1);
        titleShow = (CheckBox) findViewById(R.id.titleShow);
        notify = (CheckBox) findViewById(R.id.notify);
        plug = (CheckBox) findViewById(R.id.plug);
        autoRead = (CheckBox) findViewById(R.id.autoRead);
        transmit = (CheckBox) findViewById(R.id.transmit);
        setting = (TextView) findViewById(R.id.setting);
        textSize = (TextView) findViewById(R.id.textSize);
        text = (LinearLayout) findViewById(R.id.text);

        //开启设置以保存的设置
        settingStart();

        //监听字体大小的改变,选择方式:对话框
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.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 which) {
                        Toast.makeText(getBaseContext(),items[which].toString(),Toast.LENGTH_SHORT).show();
                        textSize.setText(items[which].toString());
                        dialog.dismiss();
                    }
                });
                builder.setCancelable(true);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });

        //监听“完成”
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settingData = new SettingData(titleShow.isChecked(),
                        textSize.getText().toString(),notify.isChecked(),
                        plug.isChecked(),autoRead.isChecked(),transmit.isChecked());
                FileOutputStream fos = null;
                ObjectOutputStream oos = null;
                String state = Environment.getExternalStorageState();
                if(!state.equals(Environment.MEDIA_MOUNTED)){
                    Toast.makeText(getBaseContext(),"请检查SD卡",Toast.LENGTH_SHORT).show();
                    return;
                }
                File file = Environment.getExternalStorageDirectory();
                try {
                    File myfile = new File(file.getCanonicalPath(),"/设置.txt");
                    fos = new FileOutputStream(myfile);
                    oos = new ObjectOutputStream(fos);
                    oos.writeObject(settingData);
                    Toast.makeText(getBaseContext(),"设置成功",Toast.LENGTH_SHORT).show();
                    oos.flush();
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos!=null){
                        try {
                            fos.flush();
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    public void settingStart(){
        //开启软件设置之前的设置
        ObjectInputStream ois = null;
        FileInputStream fis = null;
        String statu = Environment.getExternalStorageState();
        if(!statu.equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(this,"SD卡未就绪",Toast.LENGTH_SHORT).show();
            return;
        }
        File root = Environment.getExternalStorageDirectory();
        try {
            fis = new FileInputStream(root+"/设置.txt");
            ois = new ObjectInputStream(fis);
            try {
                settingData = (SettingData) ois.readObject();

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            titleShow.setChecked(settingData.isTitleShow());
            textSize.setText(settingData.getTextSize());
            notify.setChecked(settingData.isNotify());
            plug.setChecked(settingData.isPlug());
            autoRead.setChecked(settingData.isAutoRead());
            transmit.setChecked(settingData.isTransmit());
        } catch (FileNotFoundException e) {
            Toast.makeText(getBaseContext(),"未找到文件",Toast.LENGTH_SHORT).show();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null){

            }
        }


    }
}


效果图:



  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值