Android開(kāi)發(fā)文件存儲(chǔ)實(shí)例
Android的文件存儲(chǔ),有I/O流的方式存儲(chǔ),與java一樣,還有一種Android自己的SharePreferences存儲(chǔ)方法。
下面看一個(gè)例子:
用I/O流的方式存儲(chǔ)方法和SharePreferences存儲(chǔ)方法,存放QQ賬號(hào)和密碼,再次進(jìn)入頁(yè)面時(shí),把存儲(chǔ)在文件中的賬號(hào)密碼顯示在上面。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:src="@drawable/head" /> <LinearLayout android:id="@+id/ll_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/iv" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:background="#ffffff"> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="賬號(hào)" android:textColor="#000" android:textSize="20sp"/> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:padding="10dp"/> </LinearLayout> <LinearLayout android:id="@+id/ll_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/ll_number" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#ffffff"> <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="密碼" android:textColor="#000" android:textSize="20sp"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:inputType="textPassword" android:padding="10dp"/> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/ll_password" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="50dp" android:background="#3c8dc4" android:text="登錄" android:textColor="#ffffff" android:textSize="20sp" /> </RelativeLayout>
MainActivity.java
package com.example.saveqq; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText user; private EditText password; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.初始化view initView(); //2.若用戶保存了信息,進(jìn)行數(shù)據(jù)回寫(xiě) //I/O流方法 Map<String,String> userInfo = FileSaveQQ.getUserInfo(this); //SharedPreferences的方法 /* Map<String,String> userInfo = SpSaveQQ.getUserInfo(this);*/ if ((userInfo!=null)){ user.setText(userInfo.get("user")); password.setText(userInfo.get("password")); } } private void initView() { //控件的初始化 user = (EditText)findViewById(R.id.et_number); password = (EditText)findViewById(R.id.et_password); button = (Button) findViewById(R.id.btn_login); //2.設(shè)置按鈕點(diǎn)擊事件 button.setOnClickListener(this); } @Override public void onClick(View v) { //1.點(diǎn)擊獲取賬號(hào)密碼 String s_user = user.getText().toString().trim(); String s_password = password.getText().toString().trim(); //2.檢查用戶名和密碼是否為空 if (TextUtils.isEmpty(s_user)){ Toast.makeText(this,"請(qǐng)輸入QQ賬號(hào)",Toast.LENGTH_LONG).show(); return; } if (TextUtils.isEmpty(s_password)){ Toast.makeText(this,"請(qǐng)輸入QQ密碼",Toast.LENGTH_LONG).show(); return; } Toast.makeText(this,"登陸成功",Toast.LENGTH_LONG).show(); //3.保存用戶信息 //I/O流的方法 boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this,s_user,s_password); //用SharedPreferences的方法 /* boolean isSaveSuccess = SpSaveQQ.saveUserInfo(this,s_user,s_password);*/ if (isSaveSuccess){ Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this,"保存失敗",Toast.LENGTH_LONG).show(); } } }
用i/o流方法
FileSaveQQ.java
package com.example.saveqq; import android.content.Context; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FileSaveQQ { //保存QQ賬號(hào)和密碼到data.txt public static boolean saveUserInfo(Context context,String user,String password){ try { //1.通過(guò)上下文獲取文件輸出流 FileOutputStream fos = context.openFileOutput("data.txt",context.MODE_APPEND); //2.把數(shù)據(jù)寫(xiě)到文件中 fos.write((user+":"+password).getBytes()); fos.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public static Map<String,String> getUserInfo(Context context){ String content = ""; try { FileInputStream fis = context.openFileInput("data,txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); Map<String,String> userMap = new HashMap<String, String>(); content = new String(buffer); String[] infos = content.split(":"); userMap.put("user",infos[0]); userMap.put("password",infos[1]); fis.close(); return userMap; } catch (IOException e ) { return null; } } }
用SharedPreferences的方法
SpSaveQQ.java
package com.example.saveqq; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; //保存QQ賬號(hào)和密碼到data.xml中 public class SpSaveQQ { public static boolean saveUserInfo(Context context,String username,String password){ SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("username",username); editor.putString("password",password); editor.commit(); return true; } //從data.xml文件中獲取存儲(chǔ)的QQ賬號(hào)和密碼 public static Map<String,String> getUserInfo(Context context){ SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE); String username = sp.getString("username",""); String password = sp.getString("password",""); Map<String,String> userMap = new HashMap<>(); userMap.put("username",username); userMap.put("password",password); return userMap; } }
運(yùn)行截圖:
重新進(jìn)入頁(yè)面:
完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)保存QQ賬號(hào)與密碼功能(文件存儲(chǔ))
- Android 文件存儲(chǔ)與SharedPreferences存儲(chǔ)方式詳解用法
- 淺析Android文件存儲(chǔ)
- android I/0流操作文件(文件存儲(chǔ))
- 詳解Android 中的文件存儲(chǔ)
- Android存儲(chǔ)字符串?dāng)?shù)據(jù)到txt文件
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫(xiě)入存儲(chǔ)卡的方法
- Android?文件存儲(chǔ)系統(tǒng)原理
相關(guān)文章
實(shí)例講解Android中的AIDL內(nèi)部進(jìn)程通信接口使用
這篇文章主要通過(guò)實(shí)例介紹了Android中的AIDL內(nèi)部進(jìn)程通信接口使用,文中通過(guò)一個(gè)音樂(lè)播放的服務(wù)編寫(xiě)例子來(lái)講解AIDL的傳遞對(duì)象及一般使用步驟,需要的朋友可以參考下2016-04-04實(shí)例講解Android中ViewPager組件的一些進(jìn)階使用技巧
這篇文章主要介紹了Android中ViewPager組件的一些進(jìn)階使用技巧,包括添加標(biāo)題與onPagerChangeListener監(jiān)聽(tīng)使用等,需要的朋友可以參考下2016-03-03代碼實(shí)例分析android中inline hook
本片文章主要給大家通過(guò)代碼示例分析了android中inline hook的用法是實(shí)現(xiàn)過(guò)程,需要的朋友跟著參考下吧。2018-01-01Android編程實(shí)現(xiàn)監(jiān)聽(tīng)EditText變化的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)監(jiān)聽(tīng)EditText變化的方法,涉及Android針對(duì)EditText的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android中HttpURLConnection類使用介紹
早些時(shí)候其實(shí)我們都習(xí)慣性使用HttpClient,但是后來(lái)Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多開(kāi)發(fā)者放棄使用HttpClient了,HttpURLConnection畢竟是標(biāo)準(zhǔn)Java接口(java.net) ,適配性還是很強(qiáng)的2022-12-12Android利用Senser實(shí)現(xiàn)不同的傳感器
這篇文章主要為大家詳細(xì)介紹了Android利用Senser實(shí)現(xiàn)不同傳感器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android頁(yè)面中可編輯與不可編輯切換的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于在Android頁(yè)面中可編輯與不可編輯切換的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07