android记住密码 和自动登录

本文介绍了一个简单的Android登录系统实现方式,使用SharedPreferences存储用户名和密码,并实现了记住密码和自动登录的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值