安卓数据存储之SharedPreferences存储

1.知识图谱


例子1:模仿登录的记住密码

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.cookie.android0623data.LoginActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_login_uname"
        android:hint="请输入用户名"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_login_upass"
        android:hint="请输入密码"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:onClick="login"/>

</LinearLayout>


JAVA代码:

package com.example.cookie.android0623data;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText et_login_uname;
    private EditText et_login_upass;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_login_uname = (EditText) findViewById(R.id.et_login_uname);
        et_login_upass = (EditText) findViewById(R.id.et_login_upass);
        //只能够自己的文件进行读写
        sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        //想再sp里面写东西
        editor = sp.edit();

        //再次进来,获取数据
        String uname=sp.getString("uname","");
        String upass=sp.getString("upass","");
        et_login_uname.setText(uname);
        et_login_upass.setText(upass);
    }
    public void login(View view){
        //uname=admin  upass=123456
        String uname=et_login_uname.getText().toString();
        String upass=et_login_upass.getText().toString();
        Toast.makeText(this, "跳转页面", Toast.LENGTH_SHORT).show();
        editor.putString("uname",uname);
        editor.putString("upass",upass);
        //细节:
        editor.commit();


    }
}



例二,模仿手机的设置界面中的WLAN的开启和关闭

1.首先,在一个项目的res文件下建一个xml包

2.跳转到project项目下,新建一个setting.xml文件

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:title="WLAN"
        android:summaryOn="wifi已开启"
        android:summaryOff="wifi已关闭"
        android:key="@+id/sp_set_wifi"
        ></SwitchPreference>

</PreferenceScreen>



SettingActivity.java代码如下:

package com.example.cookie.android0623data;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.preference.PreferenceActivity;
import android.support.annotation.Nullable;

/**
 * Created by Administrator on 2017/6/23 0023.
 */

public class SettinActivity extends PreferenceActivity{



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //指定xml文件
        addPreferencesFromResource(R.xml.setting);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值