难道难道,一天到晚难道,今天是Anroid数据储存?(多图)

本文介绍了一个简单的安卓应用示例,展示了如何设计登录界面并使用SharedPreferences来管理用户的登录状态及偏好设置,如记住密码和自动登录等功能。

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

未来的我?不可能的 !


哇图太大了 ,这也不是我!


。不好意思下面才是我,哈哈




实例展示


代码展示

<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lenovo.spdemo.MainActivity">

    <EditText
        android:id="@+id/username_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/password_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/login_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="登录" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/rememberPsw_cb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="记住密码" />

        <CheckBox
            android:id="@+id/autoLogin_cb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="自动登录" />

    </LinearLayout>

</LinearLayout>
package com.example.lenovo.spdemo;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button loginBtn;
    private EditText usernameET;
    private  EditText  passworldET;
    private CheckBox  rememberPswCk;
    private  CheckBox autoLoginCk;


    private  int rememberFlag=0;
    private  String passworld="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindID();

        SharedPreferences sharedPreferences = getSharedPreferences("mysp.xml",MODE_PRIVATE);

        if(sharedPreferences!=null){
            String name = sharedPreferences.getString("name","");
            passworld =sharedPreferences.getString("passworld","");
            rememberFlag  = sharedPreferences.getInt("remember_flag",0);
            usernameET.setText(name);

        }
        if (rememberFlag==1){
            rememberPswCk.setChecked(true);
            passworldET.setText(passworld);
        }
        loginBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String username = usernameET.getText().toString();
                String passworld= passworldET.getText().toString();

                SharedPreferences spf = getSharedPreferences("mysp.xml",MODE_PRIVATE);
                SharedPreferences.Editor editor = spf.edit();
                editor.putString("name",username);
                editor.commit();
                if(rememberPswCk.isChecked()){
                    rememberFlag =1;
                    editor.putInt("remember_flag",rememberFlag);
                    editor.putString("password",passworld);
                }else{
                    rememberFlag =0;
                    editor.putInt("remember_flag",rememberFlag);
                }

                Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();


            }
        });
    }

    private void bindID() {
        loginBtn  =findViewById(R.id.login_btn);
        usernameET = findViewById(R.id.username_et);
        passworldET = findViewById(R.id.password_et);
        rememberPswCk = findViewById(R.id.rememberPsw_cb);
        autoLoginCk = findViewById(R.id.autoLogin_cb);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值