当用户登录和未登录时,安卓工作室按钮onclick重定向条件



我是编码新手,我需要这个特定函数的帮助,如果有人能帮忙,我将不胜感激。

我正在做一个android工作室项目,在我的活动(假定的活动A(上,我想放置一个按钮,当点击它时,它将打开假定的活动B。

我已经能够设置onclick功能,并打算在单击按钮时打开"活动B"。

但现在我想要的是,如果用户没有登录,那么点击该按钮时应该将用户带到LoginActivity,但当用户登录时,应该将用户直接带到Activity B。

请告诉我如何在我的活动中实现这一点?感谢期待解决方案。

下面是我的登录活动代码


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

import com.sckoolboy.premiumapp.R;
public class Login extends AppCompatActivity {
EditText mEmail,mPassword;
Button mCreateBtn,mLoginBtn;
TextView forgotTextLink;
ProgressBar progressBar;
FirebaseAuth fAuth;
private FirebaseAuth auth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(Login.this, Main1.class));
finish();
}
setContentView(R.layout.activity_login);
mEmail = findViewById(R.id.Email);
mPassword = findViewById(R.id.password);
progressBar = findViewById(R.id.progressBar);
fAuth = FirebaseAuth.getInstance();
mLoginBtn = findViewById(R.id.loginBtn);
mCreateBtn = findViewById(R.id.createText);
forgotTextLink = findViewById(R.id.forgotPassword);

mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)){
mPassword.setError("PremiumKey is Required.");
return;
}
if(password.length() < 6){
mPassword.setError("PremiumKey not valid");
return;
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Login.this, "Premium Successfully Unlocked", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Main1.class));
}else {
Toast.makeText(Login.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
mCreateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Register.class));
}
});
forgotTextLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText resetMail = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Reset Password ?");
passwordResetDialog.setMessage("Enter Your Email To Received Reset Link.");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// extract the email and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Login.this, "Error ! Reset Link is Not Sent" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});

}
}

使用firebase身份验证,我如何从这里开始

您可以使用if块在启动不同活动之间切换,具体取决于用户是否登录。

例如,假设loggedIn是存储用户是否登录的布尔变量,button是按钮,

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(v -> {
if (loggedIn) {
// Start ActivityB, user has logged in
Intent intent = new Intent(/* context here */, Main1.class);
startActivity(intent);
} else {
// Redirect to LoginActivity, user hasn't logged in
Intent intent = new Intent(/* context here */, Login.class);
startActivity(intent);
}
});

以及新活动的XML设计:

<!-- Root layout above..... -->
<Button
android:text="Log in"
android:id="@+id/button"/>
<!--Other elements below -->

您可以从Firebase的API获取登录条件,(参考https://stackoverflow.com/a/44585789/12204281),方法调用应为:FirebaseAuth.getInstance().getCurrentUser(),此方法在用户未登录时返回null

因此,您可以使用equals运算符检查该值是否为空,并将其存储在loggedIn:中

boolean loggedIn = FirebaseAuth.getInstance().getCurrentUser() != null;

相关内容

  • 没有找到相关文章

最新更新