如何存储在strings.xml中的用户名和密码详细信息 [英] How to store User name and password details in strings.xml

查看:409
本文介绍了如何存储在strings.xml中的用户名和密码详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,它有一个登录页面。我需要存储的登录凭据。它可以在我的strings.xml文件?因为我听说的strings.xml 不能在运行时修改。所以,我在这里可以存储数据,即用户信息或应用程序的详细信息?

I am developing an app, it has a login page. I need to store the login credentials. Can it be in my strings.xml file? Because I have heard that Strings.xml can not be modified at run time. So where can I store data i.e. User details or application details?

推荐答案

使用的共享preferences 。像这样:

创建这些方法使用,或只使用内容的方法里面,只要你想要的:

Create these methods for use, or just use the content inside of the methods whenever you want:

public String getUserName()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("userName","no userName created");
  return str;
}

public String getPassword()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("password","no password created");
  return str;
}

public void writeToUserNameAndPassword(String userName, String password)
{
  SharedPreferences.Editor pref = 
                           getSharedPreferences("userNameAndPassword",0).edit();
  pref.putString("userName", userName);
  pref.putString("password", password);
  pref.commit();
}

您可以打电话给他们这样的:

You could call them like this:

// their userName if "foo" and their password is "bar"
writeToUserNameAndPassword("foo", "bar");

if (getUserName().equals(inputUserName) && getPassword.equals(inputPassword))
{
   // they have the right userName and password
}
else if (getUserName().equals("no userName created")
         && getPassword().equals("no password created"))
{
   // these preference Strings for their userName/password have both not been created
}
else if (getUserName().equals("no userName created"))
{
   // this preference String for their userName has not been created, 
   // but the password has been
}
else if (getPassword().equals("no password created"))
{
   // this preference String for their password has not been created, 
   // but the userName has been
}
else
{
   // they entered the wrong userName and/or password
}

部分解释(如果需要):

密码username的是钥匙字符串在preference。所以,你引用这些密钥来获得字符串你把在那里。这是字符串你把一个参考名称。

"password" and "userName" are the 'key' Strings in the preference. So you reference those keys to obtain the String you put in there. It is a reference name for the String you put.

userNameAndPassword preference名称的。您可以使用preference的名字,userNameAndPassword,引用您要访问的preference。

"userNameAndPassword" is the preference name. You use the preference name, "userNameAndPassword", to reference the preference you want to access.

不创建密码不创造username的字符串,如果preference不具备的getString 方法将返回一个字符串通过引用的密码username的,这意味着它尚未创建。

"no password created" and "no userName created" are the Strings that the getString method will return if the preference doesn't have a String referenced to by "password" or "userName", meaning that it hasn't been created.

另一种方式把它:他们是参考字符串的默认值。因此,如果没有已经把他们的代替,该方法将返回默认值。你必须设置的默认值。

Another way to put it: they are the default values of the reference String. So if nothing has been put their instead, the method will return the default values. You have to set the default values.

因此​​,举例来说,如果没有密码字符串已投入了userNameAndPassword preference(写入使用 putString ),那么 getPassword来()方法将返回无密码创建

So, for example, if no "password" String has been put into the "userNameAndPassword" preference (written to using putString), then the getPassword() method will return "no password created".

这篇关于如何存储在strings.xml中的用户名和密码详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆