导包,添加依赖
找到build.gradle,后缀是自己建的项目名
// 基础依赖包,必须要依赖
implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2'
// kotlin扩展(可选)
implementation 'com.geyifeng.immersionbar:immersionbar-ktx:3.2.2'
// fragment快速实现(可选)已废弃
implementation 'com.geyifeng.immersionbar:immersionbar-components:3.2.2'
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
登陆界面
package com.example.hc4;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
EditText usernameText= findViewById(R.id.editTextTextPersonName);
EditText passwordText= findViewById(R.id.editTextTextPersonName2);
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=usernameText.getText().toString();
String password=passwordText.getText().toString();
if(username.equals("hnucm")&&password.equals("123456")){
SharedPreferences sharedPreferences = getSharedPreferences("user1",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username",username);
editor.putBoolean("isLogin",true);
editor.commit();
Intent intent = new Intent(LoginActivity2.this, MainActivity.class);
startActivity(intent);
}else {
Toast.makeText(LoginActivity2.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
}
}
});
}
}
欢迎界面
package com.example.hc4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class WelcomeActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
//等待几秒钟
//创建一个线程 ---sleep
Thread thread=new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(5000);
SharedPreferences sharedPreferences = getSharedPreferences("user1",MODE_PRIVATE);
boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
if(isLogin){
Intent intent=new Intent(WelcomeActivity2.this,MainActivity.class);
startActivity(intent);
}else {
Intent intent=new Intent(WelcomeActivity2.this,LoginActivity2.class);
startActivity(intent);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
SharedPreferences sharedPreferences = getSharedPreferences("user1",MODE_PRIVATE);
boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
if(isLogin){
Intent intent=new Intent(WelcomeActivity2.this,MainActivity.class);
startActivity(intent);
}else {
Intent intent=new Intent(WelcomeActivity2.this,LoginActivity2.class);
startActivity(intent);
}
}
}
效果展示
主界面
设立俩个按钮点击事件
package com.example.hc4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.gyf.immersionbar.ImmersionBar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImmersionBar.with(this)
// .statusBarColor(R.color.purple_200)
.init();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences;
sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username","湖南中医药大学");
// editor.remove("username");
editor.commit();
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "test123");
Log.i("hc4","username="+username+" password="+password);
}
});
}
}
好了,本周的内容就是这么多了,到这里就结束了。