01每日打卡 3月15日
今天开始正式的每日打卡的开发,首先进行了登录界面的开发,运用了sharepreference对象来存储登录信息,保证了登录只需输入一次账号和密码。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 {
EditText name,pwd;
Button btnlogin,btnreg,teacher;
Mysql mysql;
SQLiteDatabase db;
SharedPreferences sp1,sp2,sp3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = this.findViewById(R.id.name); //用户名输入框
pwd = this.findViewById(R.id.pwd); //密码输入框
btnlogin = this.findViewById(R.id.login); //登录按钮
btnreg = this.findViewById(R.id.reg); //注册按钮
teacher=this.findViewById(R.id.teacher);
sp1 = this.getSharedPreferences("userinfo",this.MODE_PRIVATE);
sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE);
sp3 = this.getSharedPreferences("user",this.MODE_PRIVATE);
boolean islogin= sp3.getBoolean("islogin",false);
name.setText(sp1.getString("usname",null));
pwd.setText(sp1.getString("uspwd",null));
mysql = new Mysql(this,"Userinfo",null,1); //建数据库或者取数据库
db = mysql.getReadableDatabase();
if(islogin==false){
btnlogin.setOnClickListener(new View.OnClickListener() { //登录事件
@Override
public void onClick(View v) {
String username = name.getText().toString();
String password = pwd.getText().toString(); //获取用户输入的用户名和密码
//查询用户名和密码相同的数据
Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null);
int flag = cursor.getCount(); //查询出来的记录项的条数,若没有该用户则为0条
if(flag!=0){ //若查询出的记录不为0,则进行跳转操作
Intent intent = new Intent();
intent.setClass(MainActivity.this,add.class); //设置页面跳转
SharedPreferences.Editor editor = sp2.edit();
cursor.moveToFirst(); //将光标移动到position为0的位置,默认位置为-1
String loginname = cursor.getString(0);
editor.putString("Loginname",loginname);
editor.commit();
SharedPreferences.Editor editor1 = sp3.edit();
editor1.putBoolean("islogin",true);
editor1.commit();
//将用户名存到SharedPreferences中
startActivity(intent);
}
else{
Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show(); //提示用户信息错误或没有账号
}
}
});
}
else{
startActivity(new Intent(MainActivity.this, add.class));
finish();
}
btnreg.setOnClickListener(new View.OnClickListener() { //注册事件
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,Register.class); //跳转到注册页面
startActivity(intent);
Toast.makeText(MainActivity.this,"前往注册!",Toast.LENGTH_SHORT).show();
}
});
teacher.setOnClickListener(new View.OnClickListener() { //注册事件
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,teacher.class); //跳转到注册页面
startActivity(intent);
Toast.makeText(MainActivity.this,"教师登录!",Toast.LENGTH_SHORT).show();
}
});
}
}
<?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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="71dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="用户名:"
android:textSize="18dp" />
<EditText
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="密 码:"
android:textSize="18dp" />
<EditText
android:id="@+id/pwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="登录" />
<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="注册" />
</LinearLayout>
<Button
android:id="@+id/teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="@color/white"
android:background="@color/light_blue"
android:text="教师登录" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具