码迷,mamicode.com
首页 > 移动开发 > 详细

Android:SharedPreferences 记住用户名和密码

时间:2015-04-15 23:03:38      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

参考: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;
    }

}

 

Android:SharedPreferences 记住用户名和密码

标签:

原文地址:http://www.cnblogs.com/Bubls/p/4430234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!