如何使用共享preF文件,我的应用程序的登录页面我的、如何使用、应用程序、页面

2023-09-12 23:01:07 作者:娇似小仙女

在我的应用我的共享pref的文件中的用户信息。当用户输入的登录页面的详细信息,我想它的信息是共享的pref的文件只用户登录,我该怎么办呢?

In my application I got user info in shared pref file. When user enter their details on the log in page, I want only users whose info is in shared pref file to log in. How can I do that?

推荐答案

您可以使用您的应用程序的共享preferences。在preFS可以随时随地使用你的这些设置键来访问的 preFS

You can use your apps shared preferences. The prefs can be accessed anytime using the key you set for these prefs.

static final String KEY_USERNAME = "username";
 static final String KEY_PASSWORD = "password";

 if (saveLogInDetail) { //save username and pw to prefs
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
      Editor ed = prefs.edit();
      ed.putString(KEY_USERNAME, theUsername);
      ed.putString(KEY_PASSWORD, thePW);
      ed.commit();
 }

要访问这些信息,或者检查有效的用户名和密码使用这样的:

To access the information or check the valid username and password use this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 String storedUsername = prefs.getString(KEY_USERNAME, "Default Value if not found");
 String storedPassword = prefs.getString(KEY_PASSWORD, ""); //return nothing if no pass saved