2024/3/15

所花时间:4小时

代码行:200行

博客量:1篇

了解到的知识点:进行安卓第一次作业的登陆界面的开发

package com.example.enroll;

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;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MainActivity extends AppCompatActivity {

private EditText mEditText_username;
private EditText mEditText_password;
private Button mButton_login;
private Button mButton_enroll1;

private String str_username;
private String str_password;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
public MySQLConnector mySQLConnector=new MySQLConnector();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText_username=findViewById(R.id.Edit_username);
mEditText_password=findViewById(R.id.Edit_password);
mButton_login=findViewById(R.id.btn_login);
mButton_enroll1=findViewById(R.id.btn_enroll1);
mSharedPreferences=getSharedPreferences("login",MODE_PRIVATE);
mEditor=mSharedPreferences.edit();

if(mSharedPreferences.getString("mEditText_username",str_username)!=null&&mSharedPreferences.getString("mEditText_password",str_password)!=null){
if(!mSharedPreferences.getString("mEditText_username",str_username).equals("")&&!mSharedPreferences.getString("mEditText_password",str_password).equals("")){
mEditText_username.setText(mSharedPreferences.getString("mEditText_username","1"));
mEditText_password.setText(mSharedPreferences.getString("mEditText_password","2"));
}
}
mButton_login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
str_username = mEditText_username.getText().toString();
str_password = mEditText_password.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
String query = "SELECT * FROM user_information WHERE user_id = ? AND user_password = ?";
PreparedStatement statement = mySQLConnector.getConn().prepareStatement(query);
statement.setString(1, str_username);
statement.setString(2, str_password);

ResultSet resultSet = statement.executeQuery();
boolean isValidLogin = resultSet.next();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (isValidLogin) {
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
mEditor.putString("mEditText_username", str_username);
mEditor.putString("mEditText_password", str_password);
mEditor.apply();
Intent intent = new Intent(MainActivity.this, user_interface.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();
}
}
});

} catch (Exception e) {

}
}
}).start();
/* if(str_username.equals("20223683")&&str_password.equals("123456")){
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
mEditor.putString("mEditText_username",str_username);
mEditor.putString("mEditText_password",str_password);
mEditor.apply();;
Intent intent=new Intent(MainActivity.this, user_interface.class);
startActivity(intent);
}else {
Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_LONG).show();
}
}
});*/
}
});
mButton_enroll1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, enroll.class);
startActivity(intent);
}
});
}
}
posted @ 2024-03-15 21:58  为20岁努力  阅读(4)  评论(0编辑  收藏  举报