日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > android小功能實現之簡單數據持久化保存(SharedPreferences)

android小功能實現之簡單數據持久化保存(SharedPreferences)

來源:程序員人生   發布時間:2015-04-08 08:40:20 閱讀次數:3073次


為了保存1些簡單的配置,類似iOS的NSUserDefault和cocos2dx的CCUserDefault,Android提供了SharedPreferences。


1 布局
先看效果圖:




打開main.xml修改內容以下:
<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_name" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/input_name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_age" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:id="@+id/input_age" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_save" android:onClick="save" android:id="@+id/button_save"/> </LinearLayout>


2 定義字符串
打開strings.xml添加內容以下:
<string name="input_name">姓名</string> <string name="input_age">年齡</string> <string name="button_save">保存參數</string> <string name="success">保存成功</string> <string name="fail">保存失敗</string>


3 功能實現
修改MainActivity.java代碼以下:
 
private EditText nameText; private EditText ageText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText = (EditText)this.findViewById(R.id.input_name); ageText = (EditText)this.findViewById(R.id.input_age); // 讀取保存的值 //SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);// 默許使用類名作為文件名稱 //SharedPreferences preferences = this.getSharedPreferences("preference", Context.MODE_PRIVATE); SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE); String name = preferences.getString("name", "空"); String age = String.valueOf(preferences.getInt("age", 0)); nameText.setText(name); ageText.setText(age); } public void save(View v){ String name = nameText.getText().toString(); Integer age = Integer.valueOf( ageText.getText().toString() ); try{ // 第1個參數為文件名稱,不能指定后綴名,第2個參數為文件操作模式 SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("name", name); editor.putInt("age", age); editor.commit(); Toast.makeText(getApplicationContext(),R.string.success, Toast.LENGTH_LONG).show(); } catch (Exception e){ Toast.makeText(getApplicationContext(),R.string.fail, Toast.LENGTH_LONG).show(); e.printStackTrace(); } }


4 運行結果

第1次運行結果如圖:


輸入內容,點擊保存參數按鈕,退出程序,再次打開,如圖:



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 色噜噜偷拍精品综合在线 | 国产激情一区二区三区在线观看 | 99成人在线视频 | 成人黄色免费网站 | 国产亚洲一区在线 | 国产成人精品视频 | 色老板视频 | 在线1区 | 亚洲国内精品 | 久草成人网| 久久国产精品一区二区 | 日本一区二区视频在线 | 国产一区二区www | 激情国产在线 | av一级毛片 | 欧美性大战久久久久久久蜜臀 | 亚洲精品一区二区三区不 | 91视频在线播放视频 | 91年国产电影大全免费观看 | 国产精品美女一区二区 | 欧美日韩在线一区二区三区 | 狠狠干综合 | 男女污视频 | 99在线精品免费视频九九视 | 久久亚洲成人 | 日韩中文字幕久久 | 久久免费视频在线 | 国产小视频在线播放 | 午夜精品久久久久久久99无限制 | 久久成人在线视频 | 亚洲国产精品第一区二区 | 91久久久久久久 | 99久久国| 国产精品久久久久久久久免费相片 | 国产精品久久久久久久久久免费看 | 99re在线精品 | 国产午夜精品久久 | 日韩精品大片 | 免费国产福利 | 国产美女一区二区 | 99视频在线 |