You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.8 KiB

package com.example.mysoft;
import android.content.Intent;
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;
import androidx.appcompat.app.AppCompatActivity;
import com.example.mysoft.activity.IndexActivity;
import com.example.mysoft.activity.RegisterActivity;
import com.example.mysoft.database.Database;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button registerButton, loginButton;
private EditText usernameText, paswdEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_login);
init();
}
private void init() {
usernameText = findViewById(R.id.username);
paswdEdit = findViewById(R.id.password);
loginButton = findViewById(R.id.login);
loginButton.setOnClickListener(this);
registerButton = findViewById(R.id.register);
registerButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
break;
case R.id.login:
//注册时,我们引入了数据库,登录这里可以通过数据库进行验证,验证跳转到首页,不通过进行提示
if (validateLogin()) {
Intent intent1 = new Intent(MainActivity.this, IndexActivity.class);
Bundle bundle = new Bundle();
Database userDatabase = new Database(MainActivity.this);
bundle.putString("username", usernameText.getText().toString());
bundle = userDatabase.queryUserInfo(userDatabase.getReadableDatabase(), bundle);
intent1.putExtras(bundle);
startActivity(intent1);
} else {
Toast.makeText(MainActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
}
break;
}
}
/**
* 登录验证
*
* @return
*/
private boolean validateLogin() {
String username = usernameText.getText().toString();
String password = paswdEdit.getText().toString();
Database userDatabase = new Database(MainActivity.this);
SQLiteDatabase sqLiteDatabase = userDatabase.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from mysoft_user where username=? and password=?", new String[]{username, password});
if (cursor.getCount() > 0) {
return true;
}
return false;
}
}
// //对话框的使用
// Button button=(Button)findViewById(R.id.fpassword);
// button.setOnClickListener(new View.OnClickListener(){
// @Override
// public void onClick(View view){
// new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
// .setMessage("忘记密码!")
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// finish();
// }
// }).setNegativeButton("返回", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
//
// }
// }).show();
// }
// });