Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 如何保存用戶設定?以實體類為媒介存入內部儲存與 SD 卡

編輯:關於 Android 程式設計

本次實例要做的,是把用戶的設定資訊,以實體類為媒介,保存到內部儲存或 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 佈局,這裡簡陋地做了一個設定介面:

                                                                

最後就是 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<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.show();="" 監聽“完成”="" setting.setonclicklistener(new="" view.onclicklistener()="" 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="" try="" myfile="new" file(file.getcanonicalpath(),"="" 設置.txt");="" fileoutputstream(myfile);="" 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){" fos.flush();="" fos.close();="" settingstart(){="" 開啟軟件設置之前的設置="" objectinputstream="" ois="null;" fileinputstream="" fis="null;" statu="Environment.getExternalStorageState();" if(!statu.equals(environment.media_mounted)){="" toast.maketext(this,"sd卡未就緒",toast.length_short).show();="" root="Environment.getExternalStorageDirectory();" fileinputstream(root+"="" objectinputstream(fis);="" ois.readobject();="" (classnotfoundexception="" titleshow.setchecked(settingdata.istitleshow());="" textsize.settext(settingdata.gettextsize());="" notify.setchecked(settingdata.isnotify());="" plug.setchecked(settingdata.isplug());="" autoread.setchecked(settingdata.isautoread());="" transmit.setchecked(settingdata.istransmit());="" (filenotfoundexception="" toast.maketext(getbasecontext(),"未找到文件",toast.length_short).show();="" (streamcorruptedexception="" if(fis!="null){" 

效果圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved