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布局,这里楚略地弄了一个设置界面:



    
        
            
                
                
                    
                    
                

            
            
            
                
                
            
            
            
                
                
                
            
            
            
                
                
                
            
            
            
                
                
                
            
            
            
                
                
                
            
            
            
                
                
            
            
            
                
                
            
            
            
                
                
            
            
            
                
                
            

        

    



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

最后,就是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

方法二:实体类保存,并将其传到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


效果图:

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

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

你可能感兴趣的:(培训日记,数据读写,设置,保存信息,IO流,实体类)