• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
宝宝儿
博客园    首页    新随笔    联系   管理    订阅  订阅

android记住密码和自动登陆

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

public class DengLu extends Activity implements OnClickListener{
private EditText name;
private EditText password;
private CheckBox box1;
private CheckBox box2;
private SharedPreferences sharedPreferences;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.denglu);
    button = (Button) findViewById(R.id.button);
    name = (EditText) findViewById(R.id.name);
    password = (EditText) findViewById(R.id.password);
    box1 = (CheckBox) findViewById(R.id.baocun);
    box2 = (CheckBox) findViewById(R.id.zidong);
    sharedPreferences = getSharedPreferences("zhuce", 0);
//记住密码
    boolean  isCheckBox1 =sharedPreferences.getBoolean("CheckBox1", false);
//自动登录
    boolean  isCheckBox2 =sharedPreferences.getBoolean("CheckBox2", false);
    
    
    
    String names=sharedPreferences.getString("name","");
    String passwords=sharedPreferences.getString("password", "");
    
    if(isCheckBox1){
            if(!names.equals("")&&!passwords.equals("")){
                name.setText(names);
                password.setText(passwords);
                box1.setChecked(true);
            }else{
                if(!names.equals("")){
                    name.setText(names);
                }
            }
    
    }
    if(isCheckBox2){
        
        Intent intent = new Intent(DengLu.this, ZhuYe.class);
        startActivity(intent);
        
    }
    button.setOnClickListener(this);
        
    
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button:
        if(box1.isChecked()){
             sharedPreferences.edit().putBoolean("CheckBox1", true).commit();
             sharedPreferences.edit().putString("name", name.getText().toString()).commit();
             sharedPreferences.edit().putString("password", password.getText().toString()).commit();
            
        }else{
            sharedPreferences.edit().putBoolean("CheckBox1", false).commit();
             sharedPreferences.edit().putString("name", name.getText().toString()).commit();
             sharedPreferences.edit().putString("password", "").commit();
        }
        if(box2.isChecked()){
            sharedPreferences.edit().putBoolean("CheckBox2", true).commit();
        }
        
        Intent intent = new Intent(DengLu.this, ZhuYe.class);
        startActivity(intent);
        break;

    default:
        break;
    }
}
}
posted @ 2016-03-25 19:14  宝宝儿  阅读(199)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3