Android:使用Sharedpreferences记录Editext输入过的内容


因为项目要记录用户上一次输入的IP,所以选择使用SharedPreferences来存储并在下次打开APP后自动写入在文本框中。

SharedPreferences介绍

SharedPreferences是使用键值对的方式来存储数据的,就是说当你保存了某条数据时,需要定义一个对应的键,然后就可以在你想要读数据的时候通过这个键所存储的数据给提取出来。

将数据保存到SharedPreferences当中

1、SharedPreferences getSharedPreferences(String name, int mode) ,这个方法有俩个参数。第一个参数为指定文件的名称,在首次运行时,会创建一个以改参数名称的文件。第二个参数为指定操作模式,MODE_PRIVATE对文件进行读写,也可以同样传入0与MODE_PRIVATE效果一样。

	    通过getSharedPreferences获取sharedPreferences对象指定文件名为DATA,对文件可读可写。
	val sm = getSharedPreferences("DATA",Context.MODE_PRIVATE)
	var editor = sm.edit()
通过putString方法将从Editext获取到的用户名以键值对为user进行存储
最后提交

	 editor.putString("user",user.text.toString())
	 editor.apply()

取出键值对的值

同样的通过getSharedPreferences获取sharedPreferences对象
        val gm = getSharedPreferences("DATA",Context.MODE_PRIVATE)
 通过调用getSring的方法来获取键值对为user所存储的值,如果没有找到相应的值则为空,并日志打印。
        var username=gm.getString("user","")
        Log.d("username:",username)

Mainactivity代码:

package com.lwh.saveeditextmessage
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Get()
        login.setOnClickListener{
            if (user.text.toString()!= null) {
                Save()
                finish()
            }

        }
    }


private fun Save(){
    val sm = getSharedPreferences("DATA",Context.MODE_PRIVATE)
    var editor = sm.edit()
    editor.putString("user",user.text.toString())
    editor.apply()
}
    private fun Get(){
        val gm = getSharedPreferences("DATA",Context.MODE_PRIVATE)
        var username=gm.getString("user","")
        Log.d("username:",username)
        if (username!=null)
        {
            user.setText(username)

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <EditText
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/user"
        app:layout_constraintVertical_bias="0.209" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="48dp"
        android:text="登录"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

</androidx.constraintlayout.widget.ConstraintLayout>

运行:

因为只是对此功能做一个简单的运用,所以代码和界面都比较简陋。望见谅!

在这里插入图片描述
首次启动输入账号后,点击登录后会退出。
再次打开后文本框已有上次输入的账号记录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值