使用SharedPreferences实现登录记住密码功能实现

具体实现方法以及步骤请看代码!

登录框 login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Account:"/>
    <EditText
        android:id="@+id/account"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" />
</LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"
            android:text="Password:"/>
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remember password"
            android:textSize="18sp" />
    </LinearLayout>
    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Login" />
</LinearLayout>

LoginActivity类
/*
    模拟简单的登陆功能
 */
public class LoginActivity extends BaseActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private CheckBox rememberPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pref = PreferenceManager.getDefaultSharedPreferences(this);//获取到SharedPreference对象
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        rememberPass = (CheckBox) findViewById(R.id.remember_pass);
        login = (Button) findViewById(R.id.login);
        boolean isRemeber = pref.getBoolean("remember_password",false);//调用它的getBoolean()方法获取remember_password,
        if (isRemeber){
            //将账号密码设置到文本框里
            String account = pref.getString("account","");
            String password = pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if (account.equals("admin") && password.equals("123456")){//登录成功之后
                    editor = pref.edit();
                    if (rememberPass.isChecked()){//检查复选框是否被选中
                        editor.putBoolean("remember_password",true);//remember_password键值就改为true
                        editor.putString("account",account);//存储账号密码
                        editor.putString("password",password);
                    }else {
                        editor.clear();
                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this, "密码或者用户名输入错误!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }                        
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的问题是如何采用SharedPreferences存储实现记住账号密码功能。下面是实现的步骤: 1. 在登录页面添加两个CheckBox,分别表示“记住账号”和“记住密码功能。 2. 在登录按钮的onClick事件中,判断CheckBox是否被选中,如果是则调用saveAccountAndPassword方法保存账号和密码。 3. 在saveAccountAndPassword方法中,使用SharedPreferences实现数据的存储。具体实现代码如下: ```java private void saveAccountAndPassword(String account, String password, boolean isRememberAccount, boolean isRememberPassword) { SharedPreferences sharedPreferences = getSharedPreferences("account_password", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); if (isRememberAccount) { editor.putString("account", account); } else { editor.remove("account"); } if (isRememberPassword) { editor.putString("password", password); } else { editor.remove("password"); } editor.apply(); } ``` 4. 在登录页面的onCreate方法中,读取SharedPreferences中保存的账号和密码,并将其设置到EditText中。具体实现代码如下: ```java private void initAccountAndPassword() { SharedPreferences sharedPreferences = getSharedPreferences("account_password", MODE_PRIVATE); String account = sharedPreferences.getString("account", ""); String password = sharedPreferences.getString("password", ""); boolean isRememberAccount = sharedPreferences.getBoolean("is_remember_account", false); boolean isRememberPassword = sharedPreferences.getBoolean("is_remember_password", false); if (isRememberAccount) { mAccountEditText.setText(account); mRememberAccountCheckBox.setChecked(true); } if (isRememberPassword) { mPasswordEditText.setText(password); mRememberPasswordCheckBox.setChecked(true); } } ``` 在上述代码中,is_remember_account和is_remember_password是用来记录CheckBox是否被选中的。 以上就是使用SharedPreferences实现记住账号密码功能的步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值