admin管理员组文章数量:1646324
模拟QQ自动登录
思想:
- 第一步:定义后台账号和密码
- 第二步:获取Shared Preference对象
final SharedPreferences sp = getSharedPreferences(“mrsoft”, MODE_PRIVATE);
第一个参数指定文件名,第二个参数指定权限。 - 第三步:实现自动登录功能,开始设置的输入内容为空
- 第四步:实现手动登录存储账号的功能通过给按钮添加点击事件,用if语句判断输入的密码是否等于设定的密码,如果是就跳转到另外一个界面就是启动一个activity,否则就打印吐司输入错误。
项目目录:
ativity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/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/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/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);
}
}
显示结果:
当输入账号密码的时候,就会跳转到另外一个界面,退出之后再进入就会自动进入界面。
版权声明:本文标题:模拟QQ自动登录 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729444824a1201327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论