SharedPreferences 案例的简单使用

本文介绍如何使用SharedPreferences在Android应用中存储和读取用户名密码,并实现从登录页面跳转到主页面显示这些信息的功能。

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

此案例为使用SharedPreferences 轻量级的存储类 ,在登陆页保存用户名密码进行跳转并将用户名密码显示在另一个Activity的Textview上。

SharedPreferences可以保存常规的Long、Int、String等类型数据

登陆页的layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <EditText
        android:id="@+id/user_password"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <Button
        android:id="@+id/login"
        android:gravity="center"
        android:text="登陆"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" 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"
    tools:context="com.example.administrator.weektext.MainActivity">
    <TextView
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="@android:color/white"
        android:background="@android:color/holo_blue_dark"
        android:id="@+id/text_name"
        android:text="衣物同"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="@android:color/white"
        android:background="@android:color/holo_blue_dark"
        android:id="@+id/text_password"
        android:text="衣物同"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
  </LinearLayout>

登陆页中使用SharePrefences

package com.example.administrator.weektext;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


//首先是登陆页
public class LoginActivity extends AppCompatActivity {

    private EditText name,password;
    private Button login;
    private String text_name,text_password;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //自定义的初始化的方法
        initView();
        //自定义的按钮的点击事件方法
        onclick();
    }

    private void onclick() {
        //按钮的点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取控件EditText中用户名和密码
                text_name=name.getText().toString();
                text_password=password.getText().toString();
                //判断用户名和密码是否有数据
                if (text_name.isEmpty()||text_password.isEmpty()){
                    Toast.makeText(LoginActivity.this,"用户名或密码为空",Toast.LENGTH_SHORT).show();
                }else {
                    //得到SharedPreferences对象,调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象
                    SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    //使用put方法将数据存入Shareprences中
                    edit.putString("name",text_name);
                    edit.putString("password",text_password);
                    edit.commit();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private void initView() {
        name = (EditText) findViewById(R.id.user_name);
        password = (EditText) findViewById(R.id.user_password);
        login = (Button) findViewById(R.id.login);

    }
}


跳转到另一个Activity并将SharePrefences中数据拿来并在本Activity中显示


package com.example.administrator.weektext;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private TextView text,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }

    private void initview() {
        text = (TextView) findViewById(R.id.text_name);
        password = (TextView) findViewById(R.id.text_password);
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        text.setText(user.getString("name",null));
        password.setText(user.getString("password",null));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值