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

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/88245995

将用户的设置信息,以实体类为媒介,保存到内部存储或者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;
    }
}

最后,就是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 = ;
                ObjectOutputStream oos = ;
                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!=){
                        try {
                            fos.flush();
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
 
    public void settingStart(){
        //开启软件设置之前的设置
        ObjectInputStream ois = ;
        FileInputStream fis = ;
        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!=){
 
            }
        }
 
 
    }
}

猜你喜欢

转载自blog.csdn.net/chaoyu168/article/details/88245995