android开发之记住密码

一、布局

1.布局代码

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="30dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号"
            android:textSize="30dp"
            android:id="@+id/etname"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="30dp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:textSize="30dp"
            android:id="@+id/etpsw"/>

    </LinearLayout>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住账号和密码"
        android:id="@+id/cb1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:layout_weight="1"
            android:text="登录"
            android:onClick="saveqq"/>
    </LinearLayout>

2.布局界面
在这里插入图片描述

二、java代码

public class two extends AppCompatActivity {
    EditText etname,etpsw;
    CheckBox cb1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);

        etname= (EditText) findViewById(R.id.etname);
        etpsw= (EditText) findViewById(R.id.etpsw);
        cb1= (CheckBox) findViewById(R.id.cb1);

        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        //取值并判断
        boolean isremember=sp.getBoolean("isremember",false);
        if(isremember){
            String name=sp.getString("name",null);
            String psw =sp.getString("psw",null);
            etname.setText(name);
            etpsw.setText(psw);
        }

    }
    public void saveqq(View V){
        SharedPreferences sp= getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor ed= sp.edit();
        //保存用户输入的用户名和密码
        ed.putString("name",etname.getText().toString());
        ed.putString("psw",etpsw.getText().toString());
        //保存一个布尔类型的值用于记录用户是否选择记住密码
        if(cb1.isChecked()){
            ed.putBoolean("isremember",true);
        }else{
            ed.clear();
        }
        ed.commit();
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现用户注册记住密码操作的步骤如下: 1. 在布局文件中添加用户名、密码记住密码的控件,例如: ``` <EditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword"/> <CheckBox android:id="@+id/rememberCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码"/> ``` 2. 在Java代码中获取这些控件,并实现记住密码的逻辑,例如: ``` public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private CheckBox mRememberCheckBox; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mUsernameEditText = findViewById(R.id.usernameEditText); mPasswordEditText = findViewById(R.id.passwordEditText); mRememberCheckBox = findViewById(R.id.rememberCheckBox); mSharedPreferences = getSharedPreferences("login", MODE_PRIVATE); if (mSharedPreferences.getBoolean("rememberPassword", false)) { mUsernameEditText.setText(mSharedPreferences.getString("username", "")); mPasswordEditText.setText(mSharedPreferences.getString("password", "")); mRememberCheckBox.setChecked(true); } } public void onLoginButtonClick(View view) { String username = mUsernameEditText.getText().toString(); String password = mPasswordEditText.getText().toString(); boolean rememberPassword = mRememberCheckBox.isChecked(); if (rememberPassword) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString("username", username); editor.putString("password", password); editor.putBoolean("rememberPassword", true); editor.apply(); } else { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.remove("username"); editor.remove("password"); editor.remove("rememberPassword"); editor.apply(); } // TODO: 实现登录逻辑 } } ``` 在上述代码中,我们通过 `getSharedPreferences()` 方法获取一个名为 "login" 的 SharedPreferences 对象来存储登录信息。当用户勾选记住密码后,在登录按钮点击事件中将用户名、密码记住密码的状态存储到 SharedPreferences 中。如果用户没有勾选记住密码,则从 SharedPreferences 中将该用户的登录信息删除。在 onCreate() 方法中,我们检查 SharedPreferences 中是否保存了登录信息,如果保存了,则将用户名、密码记住密码的状态显示在对应的控件上。 需要注意的是,我们在真正的登录逻辑中,需要根据用户输入的用户名和密码,将其与保存在服务器端的用户信息进行比对,判断是否登录成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LongTermism

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值