java - Android Studio 登录功能与数据库参考
问题描述
即使在遵循教程之后,似乎也无法破解我的头。请帮忙。
我应该改用firebase auth吗?还是我的数据库引用错误?当没有输入任何内容时,我能够启动应用程序并且错误工作正常。但是当输入用户名和密码时。什么都没发生。logcat 或活动中没有错误。
登录活动
<?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"
android:background="@drawable/loginpage"
tools:context=".loginpage">
<Button
android:id="@+id/btnSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
android:text="SignIn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="Register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="@+id/editUsername"
app:layout_constraintTop_toBottomOf="@+id/editUsername" />
<EditText
android:id="@+id/editUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="59dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="62dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
app:layout_constraintEnd_toEndOf="@+id/editUsername"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/oelogo" />
</androidx.constraintlayout.widget.ConstraintLayout>
登录 Java
FirebaseDatabase database;
DatabaseReference users;
EditText editUsername, editPassword;
Button btnSignIn, btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginpage);
database = FirebaseDatabase.getInstance();
users = database.getReference("Login");
editUsername = (EditText) findViewById(R.id.editUsername);
editPassword = (EditText) findViewById(R.id.editPassword);
btnSignIn = (Button) findViewById(R.id.btnSignIn);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent s = new Intent(getApplicationContext(), staffregisterpage.class);
startActivity(s);
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn(editUsername.getText().toString(),
editPassword.getText().toString());
}
});
}
private void signIn(final String username, final String password) {
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(username).exists()) {
if (!username.isEmpty()) {
User login = dataSnapshot.child(username).getValue(User.class);
if (login.getPassword().equals(password)) {
Toast.makeText(loginpage.this, "Success Login", Toast.LENGTH_SHORT).show();
Intent home = new Intent(getApplicationContext(),homepage.class);
startActivity(home);
} else {
Toast.makeText(loginpage.this, "Wrong Password", Toast.LENGTH_SHORT).show();
}
} else
Toast.makeText(loginpage.this, "Username not registered", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
模型/用户 Java
private String username;
private String password;
public User(String username, String password){
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
解决方案
推荐阅读
- python - 将 2D 列表项与 python 中的其他项进行比较
- bash - Bash - 如何获取位置参数的最后一个字符?
- python - `AttributeError: 'Flask' 对象没有属性 'count_obj'
- r - 如何在列表中分配更深的级别?
- kubernetes - 如何在不使用分隔符'---'的情况下将k8s中的多个部署yaml文件合并为一个yaml文件?
- javascript - 注入的 JavaScript 如何在“{}”中工作?
- c++ - QThread::exit 或 QThread::quit 不会终止派生类创建的线程
- python - 为什么过滤时会出现来自 SQLAlchemy 的 NameError?
- python - 你是怎么做计算器的?
- networking - WSL-2:自动转发哪些端口?