編輯:關於 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){"
效果圖:



Android 基礎入門教學:DrawerLayout 官方側滑選單實作
本節引言 這一節是基礎 UI 控制項的最後一位:DrawerLayout,官方提供的側滑選單控制項。它和上一節的 ViewPager 一樣是 3.0 之後才加入的,低版本
Android 自定義 PopupMenu 樣式:顯示圖示與單選按鈕
PopupMenu 是 Android 中相當輕量的一顆元件。跟 PopupWindow 比起來,它的可客製空間有限,但勝在用法簡單直覺。本文要處理的兩個痛點是:如何強制
Android 中 View 實現彈性滑動的三種方法——開發藝術探索筆記
一、什麼是彈性滑動 彈性滑動又稱漸進式滑動,做法是把一次大位移拆成許多小位移,在一段時間內逐步完成,讓滑動看起來有加速減速的過渡感,而不是硬邦邦地瞬移。所有實作方式背後都
Android 生成 keystore 的兩種方式:Eclipse 圖形化與命令行
發布前必須準備好簽名文件。這裡整理兩條路徑:一條是在 Eclipse 裡用向導點完就好,另一條是用 keytool 命令直接生成。 一、在 Eclipse 中生成 先打開