如何使用getShared preferences在安卓 [英] how to use getSharedPreferences in android

查看:206
本文介绍了如何使用getShared preferences在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,我要实现一个登录活动的应用程序。我有以下组件:

  1. 的EditText的用户名
  2. 的EditText密码
  3. 按钮登录
  4. 按钮取消

我想我的应用程序记住用户的登录信息,一旦用户登录,直至用户将preSS的注销按钮。我不使用preferences在我的XML。

我如何获得getShared preferences(字符串名称,诠释模式),在我的应用程序工作?

解决方案

 共享preferences为userDetails = context.getShared preferences(的UserDetails,MODE_PRIVATE);
编辑器编辑= userDetails.edit();
edit.clear();
edit.putString(用户名,txtUname.getText()的toString()修剪());
edit.putString(密码,txtPass.getText()的toString()修剪());
edit.commit();
Toast.makeText(背景下,登录信息将被保存。,3000).show();
 

修改

这种方式可以获取preference

 共享preferences为userDetails = context.getShared preferences(的UserDetails,MODE_PRIVATE);
字符串的uname = userDetails.getString(用户名,);
字符串传递= userDetails.getString(密码,);
 

I have an application in which I have to implement a "Login" activity. I have these components:

  1. EditText username
  2. EditText password
  3. Button Login
  4. Button Cancel

I want that my application to remember the login details of the user once the user has logged in until the user would press the "log out" button. I'm not using preferences in my xml.

How do I get the getSharedPreferences(String name, int mode) to work in my application?

解决方案

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", txtUname.getText().toString().trim());
edit.putString("password", txtPass.getText().toString().trim());
edit.commit();
Toast.makeText(context, "Login details are saved..", 3000).show();

EDIT

this way you can fetch preference

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);
String Uname = userDetails.getString("username", "");
String pass = userDetails.getString("password", "");

这篇关于如何使用getShared preferences在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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