模拟QQ自动登录

模拟QQ自动登录

思想:

  1. 第一步:定义后台账号和密码
  2. 第二步:获取Shared Preference对象
    final SharedPreferences sp = getSharedPreferences(“mrsoft”, MODE_PRIVATE);
    第一个参数指定文件名,第二个参数指定权限。
  3. 第三步:实现自动登录功能,开始设置的输入内容为空
  4. 第四步:实现手动登录存储账号的功能通过给按钮添加点击事件,用if语句判断输入的密码是否等于设定的密码,如果是就跳转到另外一个界面就是启动一个activity,否则就打印吐司输入错误。

项目目录:
在这里插入图片描述
ativity_main.xml

<?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=".MainActivity">
<EditText
    android:id="@+id/username"
    android:hint="qq/邮箱/手机号"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></EditText>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密码"></EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="150dp" />

</LinearLayout>

activity_message.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".message">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/mes"></ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>

java:
MainActivity.java

package com.example.acer.video;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //第一步:定义后台账号和密码
    private String mr = "mr";
    private String mrsoft = "mrsoft";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText userNameET = findViewById(R.id.username);  //获取账号编辑框
        final EditText userpassET = findViewById(R.id.password);  //获取密码编辑框
        Button button = findViewById(R.id.button);        //获取登录按钮
        //第二步:获取Shared Preference对象
        final SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
        //第一个参数指定文件名,第二个指定权限
        /*********************第三步:实现自动登录功能***********************************/
        String username = sp.getString("username", null);  //获取账号信息
        String password = sp.getString("password", null);  //获取密码
        if (username != null && password != null) {    //判断账号密码是否为空
            if (username.equals(mr) && password.equals(mrsoft)) {
                Intent intent = new Intent(MainActivity.this, message.class);
                startActivity(intent);
            }

        } else {
            /************************第四步:实现手动登录存储账号的功能**************************/
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String in_userName = userNameET.getText().toString(); //获取输入的账号
                    String in_password = userpassET.getText().toString();//获取输入的密码
                    SharedPreferences.Editor editor = sp.edit();  //获取editer对象
                    if (in_userName.equals(mr) && in_password.equals(mrsoft)) {
                        editor.putString("username", in_userName);   //保存账号
                        editor.putString("password", in_password);   //保存密码
                        editor.commit();    //提交信息
                        Intent intent = new Intent(MainActivity.this, message.class);
                        startActivity(intent);  //启动跳转页面
                        Toast.makeText(MainActivity.this, "已保存账号和密码", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "账号和密码错误", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }
}

message.java

package com.example.acer.video;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class message extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
    }
}

显示结果:
当输入账号密码的时候,就会跳转到另外一个界面,退出之后再进入就会自动进入界面。
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

微莱羽墨

感谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值