Sp登录记住密码

该代码段展示了如何在Android应用中创建一个登录界面,包括用户名和密码输入框,记住密码的复选框,以及登录按钮。点击登录时,如果记住密码被选中,信息将存储在SharedPreferences中。同时,应用在启动时会检查是否应自动填充之前保存的用户名和密码。
摘要由CSDN通过智能技术生成
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:padding="20dp"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">
    <EditText
        android:id="@+id/name_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"/>
    <EditText
        android:id="@+id/pass_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"/>
    </LinearLayout>
    <Button
        android:id="@+id/bt"
        android:text="登录"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
public class MainActivity2 extends AppCompatActivity {
    private EditText name_et,pass_et;
    private CheckBox cb;
    private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        name_et=findViewById(R.id.name_et);
        pass_et=findViewById(R.id.pass_et);
        cb=findViewById(R.id.cb);
        bt=findViewById(R.id.bt);
        SharedPreferences sharedPreferences = getSharedPreferences("sp", MODE_PRIVATE);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = name_et.getText().toString().trim();
                String pass = pass_et.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)){
                    Toast.makeText(MainActivity2.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    if (cb.isChecked()){
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("name",name);
                        editor.putString("pass",pass);
                        editor.putBoolean("rememberPass",true);
                        editor.apply();
                    }
                    Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
                    startActivity(intent);
                }
            }
        });
        boolean rememberPass = sharedPreferences.getBoolean("rememberPass", false);
        if (rememberPass){
            name_et.setText(sharedPreferences.getString("name",""));
            pass_et.setText(sharedPreferences.getString("pass",""));
            cb.setChecked(true);
        }

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值