Android:SharedPreferences 记住用户名和密码

参考:http://blog.csdn.net/liuyiming_/article/details/7704923

SharedPreferences介绍:

SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。

SharedPreferences的用法:

由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,用法见下面代码:

Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例

 

name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。

mode:是指定读写方式,其值有三种,分别为:

Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写

Context.MODE_WORLD_READABLE指定该SharedPreferences数据能被其他应用程序读,但不能写

Context.MODE_WORLD_WRITEABLE指定该SharedPreferences数据能被其他应用程序读写。

代码如下:

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class RegisterActivity extends Activity {

    private EditText mname,mpassword,mphone;
    private SharedPreferences sp;
    private String name,password,phone;
    private CheckBox auto_login,rb_password;
    private Button sure;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
         //获得实例对象  
        sp = this.getSharedPreferences("Login_Info", Context.MODE_PRIVATE); 
        rb_password.setChecked(true);//设置默认是记录密码状态
        auto_login.setChecked(true);//设置默认是自动登录状态
        
        mname = (EditText) findViewById(R.id.name);
        mpassword = (EditText) findViewById(R.id.password);
        mphone = (EditText) findViewById(R.id.phonenumber);
        sure = (Button)findViewById(R.id.sure);
     auto_login = (CheckBox) findViewById(R.id.auto_log);
     rb_password = (CheckBox) findViewById(R.id.rb_pw);
        sure.setOnClickListener(new myButtonListener1() );
        
    }
    
    class myButtonListener1 implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Remember();
        }    
        
    }
    public void Check_Login_State(){
        //判断记住密码多选框的状态
          if(sp.getBoolean("ISCHECK", true))
            {
        //获取sp中的用户信息
              mname.setText(sp.getString("Login_Name", ""));
              mpassword.setText(sp.getString("Login_Password", ""));
                 //判断自动登陆多选框状态
                 if(sp.getBoolean("AUTO_ISCHECK", true))
                     {
                     //跳过登录界面
                     }
            }
    }
    
    public void Remember(){
        name = mname.getText().toString();
        password = mpassword.getText().toString();
        phone = mphone.getText().toString();
        //记住用户名、密码、
          Editor editor = sp.edit();
          editor.putString("Login_Name", name);
          editor.putString("Login_Password",password);
          editor.putString("Login_Phone", phone);
          editor.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.register, menu);
        return true;
    }

}

 

转载于:https://www.cnblogs.com/Bubls/p/4430234.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值