Android的共享preferences找回用户名和密码用户名、密码、Android、preferences

2023-09-05 11:07:23 作者:鼻涕大佬

林其与Android的共享preferences获取用户名和密码的麻烦。我用这个code保存的用户名,并通过

Im having trouble with retrieving username and password from android's sharedpreferences. I use this code to save the username and pass

SharedPreferences prefs=getSharedPreferences("File", 0);
    SharedPreferences.Editor e=  prefs.edit();
       e.putString("Email", "example@example.com").putString("Password", "password1");
       e.commit();
       e.putString("Email", "example_2@example.com").putString("Password", "password2");
       e.commit();
       String s=prefs.getString("Email","not found");

但我不知道如何获取信息的用户进行登录。任何人都可以帮我找出

But i dont know how to retrieve information for user to log in. Can anybody help me figure out

推荐答案

创建共享preference:

SharedPreferences sp=getSharedPreferences("Login", 0);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("Unm",Value );              
Ed.putString("Psw",Value);   
Ed.commit();

从分享preference获得值:

SharedPreferences sp1=this.getSharedPreferences("Login",null);

String unm=sp1.getString("Unm", null);       
String pass = sp1.getString("Psw", null);