Android实现登录界面以及保存密码

Android项目逻辑。

一,画ui。

二,根据ui写逻辑,写出布局代码。

?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_username"
    android:hint="用户名"/>
    <EditText
        android:layout_marginTop="10dp"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_password"
        android:hint="密码"/>
<RelativeLayout
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
        android:id="@+id/cb_ischeck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"/>
    <Button
        android:layout_marginRight="20dp"
        android:id="@+id/login"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"/>
</RelativeLayout>
</LinearLayout>

 三,写事件处理。

package com.tjgx.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText username,password;    //账号密码编辑框
    private CheckBox checkBox;    //记住密码框
    private Button login;   //登录按钮
   private SharedPreferences sp; //保存用的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp =getSharedPreferences("config",0); //SharedPreferences初始化
        initview();
    }

    private void initview() {
        //找到需要控件。
        username = findViewById(R.id.et_username);
        password = findViewById(R.id.et_password);
        checkBox = findViewById(R.id.cb_ischeck);
        login = findViewById(R.id.login);
        login.setOnClickListener(new  MyonclickListener());
        //从保存的文件中取出账号密码
        String s1=sp.getString("name","");
        String s2=sp.getString("pwd","");
        username.setText(s1);
        password.setText(s2);
    }
    class MyonclickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            String name = username.getText().toString().trim();
            String pwd = password.getText().toString().trim();
            if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
              //  Sharedpreferences.putString(MainActivity.this,name,pwd);
                Log.v("log","登录失败");
                Toast.makeText(MainActivity.this,"账号或密码为空",Toast.LENGTH_LONG).show();
            }else{
                Log.e("login","登录成功"+name+pwd);
                Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();

            if (checkBox.isChecked()){
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("name",name);
                edit.putString("pwd",pwd);
                edit.apply();

            }
        }
        }
    }
}

四,打包程序。

项目中SharedPreferences是Android提供的保存数据的。

使用方法分4步:

1,获取实例对象

sp =getSharedPreferences("config",0); //SharedPreferences初始化

2.获取编辑器

SharedPreferences.Editor edit = sp.edit();

3.存数据

edit.putString("name",name);
edit.putString("pwd",pwd);

4.应用生效

edit.apply();

当你把记住密码勾上之后,下次进入记录上次数据。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会写Java的小疯子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值