SharedPreferences

本文介绍SharedPreferences的基本概念及使用方法,包括如何存储和读取数据,并通过一个记住密码的案例演示具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.SharedPreferences是什么

SharedPreferences是一种轻量级的数据存储方式,用来保存应用的一些常用配置,它是Android数据持久化方法中最简单的一种。

2.SharedPreferences如何存储数据,如何读取数据

1、根据Context获取SharedPreferences对象
2、利用edit()方法获取Editor对象。
3、通过Editor对象存储key-value键值对数据。
4、通过commit()方法提交数据。

使用SharedPreferences储存存数据方法如下:

//实例化SharedPreferences对象(第一步) 
SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); 

//实例化SharedPreferences.Editor对象(第二步) 
SharedPreferences.Editor editor = mySharedPreferences.edit(); 

//用putString的方法保存数据 
editor.putString("name", "Karl"); 
editor.putString("habit", "sleep"); 

//提交当前数据 
editor.commit(); 

//使用toast信息提示框提示成功写入数据 
Toast.makeText(this, "数据成功写入SharedPreferences!" , Toast.LENGTH_LONG).show();

使用SharedPreferences读取数据方法如下:

/同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象 
SharedPreferencessharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); 

// 使用getString方法获得value,注意第2个参数是value的默认值 
String name =sharedPreferences.getString("name", ""); 
String habit =sharedPreferences.getString("habit", ""); 

//使用toast信息提示框显示信息 
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,Toast.LENGTH_LONG).show(); 

3.记住密码案例

布局文件:

<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"
    android:background="#f4efef"
    tools:context="com.example.dfcn.paopao.LoginActivity">
   <LinearLayout
       android:background="#f9f74922"
       android:layout_width="match_parent"
       android:layout_height="45dp">
       <LinearLayout
           android:layout_gravity="center_vertical"
           android:layout_weight="1"
           android:layout_width="10dp"
           android:layout_height="wrap_content">
           <ImageView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@mipmap/back_btn"/>
       </LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="登 陆"
        android:textSize="25sp" />
</LinearLayout>

   </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="15dp">

</LinearLayout>
<LinearLayout
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_number"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机:"/>
        <EditText
            android:id="@+id/login_iphone_et"
            android:layout_toRightOf="@id/tv_number"
            android:hint="请输入手机号"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_number"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:background="#dedddd"
            android:layout_width="match_parent"
            android:layout_height="1dp" />
    </RelativeLayout>

    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_pwd"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:id="@+id/login_pwd_et"
            android:layout_toRightOf="@id/tv_pwd"
            android:hint="请输入密码"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_pwd"/>
    </RelativeLayout>

</LinearLayout>
    <CheckBox
        android:id="@+id/cb_pwd"
        android:text="记住密码"
        android:layout_width="match_parent"
        android:layout_height="30dp"/>
<LinearLayout
    android:layout_marginTop="30sp"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
     <Button
         android:textColor="#ffff"
         android:background="#f9f74922"
         android:id="@+id/btn_login"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="登陆"
         />
       <Button
           android:textColor="#ffff"
           android:background="#f9f74922"
           android:id="@+id/btn_register"
           android:layout_marginLeft="80dp"
           android:layout_toRightOf="@id/btn_login"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="注册"/>
   </RelativeLayout>
</LinearLayout>


</LinearLayout>

activity:

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class passwordActivity extends AppCompatActivity {
private EditText iphonenum,pwd;
private Button loginbtn;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        iphonenum=findViewById(R.id.login_iphone_et);
        pwd=findViewById(R.id.login_pwd_et);
        loginbtn=findViewById(R.id.btn_login);
        checkBox=findViewById(R.id.cb_pwd);
        sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
        //CheakBox的状态
        boolean isRemenber=sharedPreferences.getBoolean("checkBox",false);
        //如果勾选了下次在进入就把保存的数据给读取出来
        if (isRemenber==true){
            String usename=sharedPreferences.getString("usename","");
            String pwds=sharedPreferences.getString("pwds","");
            iphonenum.setText(usename);
            pwd.setText(pwds);
            checkBox.setChecked(true);
        }
        //登陆的单击事件
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usename=iphonenum.getText().toString();
                String pwds=pwd.getText().toString();
                editor=sharedPreferences.edit();
                //判断是否勾上了记住密码
               if (checkBox.isChecked()){
                   editor.putString("usename",usename);
                   editor.putString("pwds",pwds);
                   editor.putBoolean("checkBox",true);
               }
               else {
                   editor.clear();
               }
                    editor.commit();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();

            }
        });
    }
}
内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
资 源 简 介 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系, 详 情 说 明 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。 本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系,在此基础上重点分析了一种快速ICA实现算法一FastICA。物质的非线性荧光谱信号可以看成是由多个相互独立的源信号组合成的混合信号,而这些独立的源信号可以看成是光谱的特征信号。为了更好的了解光谱信号的特征,本文利用独立分量分析的思想和方法,提出了利用FastICA算法提取光谱信号的特征的方案,并进行了详细的仿真实验。 此外,我们还进行了进一步的研究,探索了其他可能的ICA应用领域,如音乐信号处理、图像处理以及金融数据分析等。通过在这些领域中的实验和应用,我们发现ICA在提取信号特征、降噪和信号分离等方面具有广泛的潜力和应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值