android记住登录信息登录状态 使用SharePreference接口

public class MainActivity extends AppCompatActivity {    EditText ueditText, peditText;    CheckBox checkBox;    Button button;    /**     * ATTENTION: This was auto-generated to implement the App Indexing API.     * See https://g.co/AppIndexing/AndroidStudio for more information.     */    private GoogleApiClient client;

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ueditText = (EditText) findViewById(R.id.ed_user);        peditText = (EditText) findViewById(R.id.ed_password);        checkBox= (CheckBox) findViewById(R.id.cb_remember);        button= (Button) findViewById(R.id.button);        //使用SharePreferences取出保存的数据,并把数据显示在手机屏幕上        //初始化数据        SharedPreferences sharedPreferences=getSharedPreferences("config",0);        //取出数据,如果取出的数据时空时,只需把getString("","")第二个参数设置成空字符串就行了,不用在判断        String name=sharedPreferences.getString("name","");        String password=sharedPreferences.getString("password","");        //获取勾选的状态        boolean checkbox=sharedPreferences.getBoolean("checkbox",false);        ueditText.setText(name);        peditText.setText(password);        checkBox.setChecked(checkbox);    }    //使用Sharepreferences进行保存数据   public void login(View view){       //获取密码和用户名       String username=ueditText.getText().toString();       String passwowrd=peditText.getText().toString();       //文本判断是否为空,新的API:TextUtils.isEmty()       if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(passwowrd)){           Toast.makeText(MainActivity.this,"用户名和密码不能为空",Toast.LENGTH_LONG).show();       }else{           System.out.println("以后补上");           if (checkBox.isChecked()){               //把密码和用户名存起来               //getSharedPreferences(name,model);,name 会生成一个xml文件,model :模式,可读可写等模式               SharedPreferences sp=getSharedPreferences("config",0);               SharedPreferences.Editor editor=sp.edit();               //把数据进行保存               editor.putString("name",username);               editor.putString("password",passwowrd);               //记住勾选的状态               editor.putBoolean("checkbox",checkBox.isChecked());               //提交数据               editor.commit();           }else{               Toast.makeText(MainActivity.this,"未勾选",Toast.LENGTH_LONG).show();           }       }   }    /**     * Sharepreference使用的步骤     * 1.获取sp的实例     * Sharepreference sp=getSharepreference(name,model);     * 2.获取编辑器     * Editor editor=sp.edit();     * 3.存数据     * editor.putString(name,值)     * 4.提交     * editor.commit();     */}xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.hx.myapplication.MainActivity">

   <EditText       android:id="@+id/ed_user"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:hint="@string/husername"/>    <EditText        android:id="@+id/ed_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:password="true"        android:hint="@string/hpsword"/>    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <CheckBox            android:layout_marginTop="20dp"            android:id="@+id/cb_remember"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/remember"/>        <Button            android:id="@+id/button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/cb_remember"            android:text="@string/login"            android:onClick="login"/>    </RelativeLayout></LinearLayout>
 
时间: 2024-08-27 15:51:44

android记住登录信息登录状态 使用SharePreference接口的相关文章

Android记住密码自动登录的实现

我采用的是SharedPreferences来存取数据的,所以先简单的介绍一下SharedPreferences SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下. 获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法

Cookie中用户登录信息登录验证

public class FormServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletR

jsp Cookie记住用户的登录状态

Login.jsp <%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.get

Asp.Net MVC记住用户登录信息 下次登录无需输入密码

有的时候做网站,就需要记住用户登录信息,下次再登录网站时,不用重复输入用户名和密码,原理是浏览器的cookie把状态给记住了! 那么具体是怎么实现的呢?下面博主将一部分代码贴出来,想要完整版的Demo可以到百度云和码云下载, 百度云下载链接:https://pan.baidu.com/s/1rfQNxpM8WGxkBbdiS1mLeQ    密码:if1q 马云下载链接:https://gitee.com/WuFengZui/RememberLoginDemo    [没有下载链接的都是耍流氓 

Android之QQ授权登录获取用户信息

有时候我们开发的app须要方便用户简单登录.能够让用户使用自己的qq.微信.微博登录到我们自己开发的app. 今天就在这里总结一下怎样在自己的app中集成QQ授权登录获取用户信息的功能. 首先我们打开腾讯开发平台这个网页,点击---->移动应用---->创建应用,成功创建应用后.能够产生我们须要的App ID和App Key,例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVhcl9odWFuZ3poZW4=/font/5a6

jQuery实例-记住登录信息

本文介绍下jquery 记住登录信息的方法,引入jquery.cookie.js文件,实现记住登录信息,有需要的朋友参考下. 首先,导入jquery.cookie.js $(function(){ //初始化页面时验证是否记住了密码 if ($.cookie("autologin") == "true") { $("#autologin").attr("checked", true); $("#account&quo

Android记住密码后自动登录

/** * * @author alex * @version 2014-7-31下午5:25:45 * */ public class LoginActivity extends Activity { private EditText name; private EditText pass; private CheckBox isRemenber; private CheckBox isLoginSelf; private Button longin; private ProgressDial

使用cookie记录登录名,下次登录时能够记得上次的登录名,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <

C# winform实现记住用户登录的登录状态

namespace DMS { public class LoginInfo { public string AccountID { set; get; } public string AccountPassWord { set; get; } public DateTime AccountLoginTime { set; get; } private static LoginInfo _CurrentUser = null; public static LoginInfo CurrentUse