无法检索共享的首选项数据

问题描述 投票:0回答:1
package com.example.myapplication

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.icu.util.VersionInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.Sampler
import android.text.LoginFilter
import android.util.Log
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.LinkedBlockingDeque

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val username = findViewById<EditText>(R.id.ET1).text
        val password = findViewById<EditText>(R.id.ET2).text

        val username1 = getSharedPreferences("username", Context.MODE_PRIVATE)
        username1.edit().putString("username", username.toString()).apply()

        val password1 = getSharedPreferences("password", Context.MODE_PRIVATE)
        password1.edit().putString("password", password.toString()).apply()
        /*val rememberme = findViewById<CheckBox>(R.id.checkBox)*/
        val loginbutton = findViewById<Button>(R.id.LoginButton)
        /*val register = findViewById<TextView>(R.id.textView2)*/

        loginbutton.setOnClickListener {
            Log.i("username is ",username1.getString("username","").toString())
            Log.i("username is ",password1.getString("password","").toString())
            val intent = Intent(this, ListActivity::class.java)
            startActivity(intent)
        }



    }

}

问题:无法记录我要求用户在用户名和密码字段中输入的数据。

错误:

没有这样的错误,但是日志不显示用户名或密码。基本上,我正在尝试向应用程序添加登录功能。

SDK版本为28

android kotlin android-edittext sharedpreferences
1个回答
0
投票
 SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
 SharedPreferences.Editor sPref = prefs.edit(); 
 sPref.putString("username", uName);
 sPref.putString("password", pWord);
 sPref.commit();
© www.soinside.com 2019 - 2024. All rights reserved.