gpt4 book ai didi

android - 如何在 Firebase 的对话框登录下方添加忘记密码链接?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:37 28 4
gpt4 key购买 nike

Screenshot of dialog box

我正在警报对话框中创建登录表单,但在此对话框中,我想在登录下方添加忘记密码链接,这会将我链接到另一个 Activity 。我尝试了很多修改,但它崩溃了。我在这个应用程序中使用 Firebase 身份验证。当我修改主要 Activity 中的代码并给出错误时,应用程序崩溃。下面是我的主要 java 代码。

 package com.techno.giants.technogiants;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.techno.giants.technogiants.Model.User;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public class MainActivity extends AppCompatActivity {

RelativeLayout rootLayout;
Button btnSignIn,btnRegister;
FirebaseAuth auth;
FirebaseAuth.AuthStateListener mAuthListener;
FirebaseDatabase db;
DatabaseReference users;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Arkhip_font.ttf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_main);
//init firebase
auth = FirebaseAuth.getInstance();
db=FirebaseDatabase.getInstance();
users=db.getReference("Users");


btnRegister=(Button)findViewById(R.id.btnRegister);
btnSignIn=(Button)findViewById(R.id.btnSignIn);
rootLayout=(RelativeLayout)findViewById(R.id.rootLayout);

//Event
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRegisterDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showLoginDialog();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (auth.getCurrentUser() !=null){
Intent intent=new Intent(MainActivity.this,Welcome.class);
startActivity(intent);
}
}
};

}

private void showLoginDialog() {
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("SIGN IN");
dialog.setMessage("Please use Email to Sign In");

LayoutInflater inflater=LayoutInflater.from(this);
View login_layout=inflater.inflate(R.layout.layout_login,null);

final MaterialEditText edtEmail=login_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword=login_layout.findViewById(R.id.edtPassword);


dialog.setView(login_layout);

//set button
dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

dialogInterface.dismiss();
//check validation
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter Email Address", Snackbar.LENGTH_SHORT)
.show();
return;
}

if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter Password", Snackbar.LENGTH_SHORT)
.show();
return;
}
if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short", Snackbar.LENGTH_SHORT)
.show();
return;
}

//login
auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivity.this,Welcome.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed sign In"+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});

}

});

dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});


dialog.show();

}



private void showRegisterDialog() {
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use Email to register");

LayoutInflater inflater=LayoutInflater.from(this);
View register_layout=inflater.inflate(R.layout.layout_register,null);

final MaterialEditText edtEmail=register_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword=register_layout.findViewById(R.id.edtPassword);
final MaterialEditText edtName=register_layout.findViewById(R.id.edtName);
final MaterialEditText edtPhone=register_layout.findViewById(R.id.edtPhone);
final MaterialEditText edtCollege=register_layout.findViewById(R.id.edtCollege);

dialog.setView(register_layout);

//set button
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

dialogInterface.dismiss();
//check validation
if (TextUtils.isEmpty(edtEmail.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Email Address",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPhone.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Phone Number",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Password",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (edtPassword.getText().toString().length()<6)
{
Snackbar.make(rootLayout,"Password too short",Snackbar.LENGTH_SHORT)
.show();
return;
}
//register new user
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//save user to db
User user=new User();
user.setEmail(edtEmail.getText().toString());
user.setName(edtName.getText().toString());
user.setPhone(edtPhone.getText().toString());
user.setPassword(edtPassword.getText().toString());
user.setCollege(edtCollege.getText().toString());



//use email to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout,"Register success",Snackbar.LENGTH_SHORT)
.show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_LONG)
.show();
}
});
}
})

.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();

}

@Override
protected void onStart() {
super.onStart();
auth.addAuthStateListener(mAuthListener);
}
}

最佳答案

可以输入日志文件吗?

一些特殊字符(.)没有在 FireBase 中输入。

关于android - 如何在 Firebase 的对话框登录下方添加忘记密码链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47466308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com