日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android数据存储之SharePreference和内部存储

發布時間:2024/7/5 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android数据存储之SharePreference和内部存储 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android中的數據存儲

*存儲方式
1.SharedPreference存儲:簡單的信息...
2.內部存儲:保存在app內部存儲空間,非公開
3.外部存儲:公共空間
4.數據庫存儲:結構化數據
5.網絡存儲:云....

1.SharedPreference的用法

案列效果:點擊保存數據會將數據保存data1文件中并顯示保存成功且情況編輯框中內容,點擊恢復數據則將之前填寫的數據恢復到編輯框中

布局文件中:

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:" /><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:drawable/editbox_background"android:hint="請輸入姓名" android:textSize="20sp"/> <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="年齡:" /><EditTextandroid:id="@+id/et_age"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:drawable/editbox_background"android:hint="請輸入年齡" android:textSize="20sp"/> <LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:orientation="horizontal"><Button android:id="@+id/btn_save"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="#FA8072"android:text="保存數據"/><Button android:id="@+id/btn_resume"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="#FA8072"android:layout_marginLeft="5dp"android:text="恢復數據"/> </LinearLayout>

java代碼中;

public class MainActivity extends Activity implements OnClickListener{private EditText et_name,et_age;private Button btn_save,btn_resume;private SharedPreferences sp;//使用鍵值對的方式存儲數據,相當于紙@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();btn_save.setOnClickListener(this);btn_resume.setOnClickListener(this);sp = getSharedPreferences("data1", MODE_PRIVATE);//第一個參數是保存的文件名,第二個是操作模式}private void initView() {et_name = (EditText) findViewById(R.id.et_name);et_age = (EditText) findViewById(R.id.et_age);btn_save = (Button) findViewById(R.id.btn_save);btn_resume = (Button) findViewById(R.id.btn_resume);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_save:String name = et_name.getText().toString().trim();//獲取填寫的內容并去掉空格String age = et_age.getText().toString().trim();if(TextUtils.isEmpty(name)||TextUtils.isEmpty(age)){return; //若填寫的內容有一個為空則退出,TextUtils.isEmpty()方法可以一次性進行兩種空值的判斷,當傳入的字符串等于null或者等于空字符串時,該方法都返回true}Editor edit = sp.edit();//獲得編輯器--->筆edit.putString("name",name);edit.putString("age", age);boolean commit = edit.commit();//提交--->保存if(commit){Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();et_name.setText("");et_age.setText("");}break;case R.id.btn_resume:String nameValue = sp.getString("name", "");String ageValue = sp.getString("age", "");et_name.setText(nameValue);et_name.setSelection(nameValue.length());//setSelection方法將光標移動到文本末尾以便繼續輸入et_age.setText(ageValue);et_age.setSelection(ageValue.length());break;} } }

*2.內部存儲的用法
主要方法:openFileOutput(文件名,操作模式):返回一個FileOutputStream對象
openFileInput(文件名):返回一個FileInputStream對象
deleteFile(文件名):刪除文件
文件保存位置:/data/data/包名/files/...
內部存儲特點:內部存儲的東西會隨著app的卸載而被清掉

案列效果:實現保存,打開,刪除文件功能xml布局:<LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><EditText android:id="@+id/et_filename"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:drawable/editbox_background"android:padding="10dp"android:hint="請輸入文件名"android:textSize="20sp"android:singleLine="true"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:drawable/editbox_background_normal"android:onClick="savefile"android:text="保存"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:drawable/editbox_background_normal"android:onClick="openfile"android:text="打開"/> </LinearLayout><EditText android:id="@+id/et_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:hint="請輸入內容"android:textSize="20sp"android:background="@android:drawable/editbox_background"/> <Button android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="delfile"android:text="刪除文件"android:background="@android:drawable/editbox_background"/>

Java代碼中:

public class MainActivity extends Activity {private EditText et_filename,et_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {et_filename = (EditText) findViewById(R.id.et_filename);et_content = (EditText) findViewById(R.id.et_content);}//保存文件public void savefile(View v){String filename = et_filename.getText().toString().trim();String content = et_content.getText().toString().trim();if(TextUtils.isEmpty(filename)||TextUtils.isEmpty(content)){return;}FileOutputStream out=null;BufferedWriter bw=null;try {out = openFileOutput(filename,MODE_PRIVATE);bw = new BufferedWriter(new OutputStreamWriter(out));bw.write(content);et_filename.setText("");et_content.setText("");} catch (Exception e) {e.printStackTrace();}finally{try {if(bw!=null){bw.close();}} catch (IOException e) {e.printStackTrace();}}Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();}//打開文件public void openfile(View v){String filename = et_filename.getText().toString().trim();if(TextUtils.isEmpty(filename)){return;}FileInputStream in=null;BufferedReader br=null;StringBuilder content = new StringBuilder();try {in = openFileInput(filename);br = new BufferedReader(new InputStreamReader(in));String line = "";while((line = br.readLine())!=null ){content.append(line);}et_content.setText(content);} catch (Exception e) {e.printStackTrace();}finally{try {if(br!=null){br.close();}} catch (IOException e) {e.printStackTrace();}} }//刪除文件public void delfile(View v){String filename = et_filename.getText().toString().trim();if(TextUtils.isEmpty(filename)){return;}boolean deleteFile = deleteFile(filename);if(deleteFile){Toast.makeText(MainActivity.this, "刪除成功", Toast.LENGTH_SHORT).show();et_filename.setText("");et_content.setText("");} } }

轉載于:https://www.cnblogs.com/SanguineBoy/p/9895182.html

總結

以上是生活随笔為你收集整理的Android数据存储之SharePreference和内部存储的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。