最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)文件存儲實(shí)例

 更新時間:2021年11月09日 17:00:24   作者:Chasing stars  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)文件存儲實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android的文件存儲,有I/O流的方式存儲,與java一樣,還有一種Android自己的SharePreferences存儲方法。

下面看一個例子:

用I/O流的方式存儲方法和SharePreferences存儲方法,存放QQ賬號和密碼,再次進(jìn)入頁面時,把存儲在文件中的賬號密碼顯示在上面。

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="賬號"
            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ù)回寫
        //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)擊獲取賬號密碼
        String  s_user = user.getText().toString().trim();
        String  s_password = password.getText().toString().trim();
        //2.檢查用戶名和密碼是否為空
        if (TextUtils.isEmpty(s_user)){
            Toast.makeText(this,"請輸入QQ賬號",Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(s_password)){
            Toast.makeText(this,"請輸入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賬號和密碼到data.txt
    public static  boolean saveUserInfo(Context context,String user,String password){
        try {
            //1.通過上下文獲取文件輸出流
            FileOutputStream fos = context.openFileOutput("data.txt",context.MODE_APPEND);
            //2.把數(shù)據(jù)寫到文件中
            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賬號和密碼到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文件中獲取存儲的QQ賬號和密碼
    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)入頁面:

完成。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例

    android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例

    本篇文章主要介紹了android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android開發(fā)入門之Service用法分析

    Android開發(fā)入門之Service用法分析

    這篇文章主要介紹了Android中Service用法,較為詳細(xì)的分析了Service的功能、相關(guān)函數(shù)使用及注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置

    Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置

    這篇文章主要介紹了Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Android開發(fā)技巧之Fragment的懶加載

    Android開發(fā)技巧之Fragment的懶加載

    我們都知道fragment放在viewPager里面,viewpager會幫我們預(yù)先加載一個,但是當(dāng)我們要看fragment里面的內(nèi)容時,我們也許只會去看第一個,不會去看第二個,如果這時候不去實(shí)現(xiàn)fragment的懶加載的話,就會多余的去加載一些數(shù)據(jù),造成用戶多消耗流量。下面來一起看看吧。
    2016-10-10
  • Android自定義ScrollView使用自定義監(jiān)聽

    Android自定義ScrollView使用自定義監(jiān)聽

    這篇文章主要介紹了Android自定義ScrollView使用自定義監(jiān)聽 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android 使用幀動畫內(nèi)存溢出解決方案

    Android 使用幀動畫內(nèi)存溢出解決方案

    這篇文章主要介紹了Android 使用幀動畫內(nèi)存溢出解決方案的相關(guān)資料,這里提供了詳細(xì)的解決辦法,具有參考價值,需要的朋友可以參考下
    2016-12-12
  • Android WebView的使用方法及與JS 相互調(diào)用

    Android WebView的使用方法及與JS 相互調(diào)用

    這篇文章主要介紹了Android WebView的使用方法及與JS 相互調(diào)用的相關(guān)資料,WebView 是 Android 中一個非常實(shí)用的組&#8203;件, WebView 可以使得網(wǎng)頁輕松的內(nèi)嵌到app里,還可以直接跟js相互調(diào)用,需要的朋友可以參考下
    2017-07-07
  • android實(shí)現(xiàn)輪播圖引導(dǎo)頁

    android實(shí)現(xiàn)輪播圖引導(dǎo)頁

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)輪播圖引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • android獲取時間差的方法

    android獲取時間差的方法

    這篇文章主要介紹了android獲取時間差的方法,涉及Android操作時間的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 探討:如何修改Android超時休眠時間

    探討:如何修改Android超時休眠時間

    本篇文章是對如何修改Android超時休眠時間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

乌鲁木齐市| 闵行区| 资溪县| 甘泉县| 兴宁市| 青浦区| 利津县| 平阳县| 从江县| 宜川县| 紫金县| 高密市| 诸暨市| 明光市| 兴山县| 苍梧县| 德庆县| 柳江县| 岳阳市| 嘉黎县| 古田县| 泗阳县| 阜平县| 新晃| 克山县| 呼和浩特市| 上林县| 长春市| 攀枝花市| 彭泽县| 施秉县| 常山县| 县级市| 洛扎县| 平度市| 噶尔县| 康保县| 华安县| 仁寿县| 辽宁省| 荔浦县|