我的按钮在字段填满时不会有反应。

huangapple go评论50阅读模式
英文:

My buttons doesn't react when fields are full

问题

当我在文本字段中输入信息时,当我单击按钮时,按钮不会响应。数据会传递到 Firebase,但不会影响应用程序的工作方式。如果文本字段为空,我的 Toast 消息有效(按钮会响应),但当它们被填满时,屏幕上什么都不会发生。在注册或登录时,屏幕应该跳转到主活动,但什么都没有发生。以下是我的代码:

LoginActivity

class LoginActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        auth = Firebase.auth

        var signuplinkbtn = findViewById<Button>(R.id.signup_link_btn)

        signuplinkbtn.setOnClickListener {
            startActivity(
                Intent(
                    this,
                    RegisterActivity::class.java
                )
            )
        }

        val loginButton = findViewById<Button>(R.id.login_btn)

        loginButton.setOnClickListener {
            performLogin()
        }
    }

    private fun performLogin() {
        val email = findViewById<EditText>(R.id.email_login)
        val password = findViewById<EditText>(R.id.password_login)

        if (email.text.isEmpty() || password.text.isEmpty()){
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_LONG).show()
            return
        }
        val emailInput = email.text.toString()
        val passwordInput = password.text.toString()

        auth.signInWithEmailAndPassword(emailInput, passwordInput)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // 登录成功,跳转到主活动
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)

                    Toast.makeText(
                        baseContext,
                        "登录成功",
                        Toast.LENGTH_SHORT,
                    ).show()

                } else {
                    // 如果登录失败,向用户显示消息
                    Toast.makeText(
                        baseContext,
                        "身份验证失败",
                        Toast.LENGTH_SHORT,
                    ).show()
                }
            }
            .addOnFailureListener {
                Toast.makeText(this, "发生错误", Toast.LENGTH_LONG).show()
            }
    }
}

RegisterActivity:

class RegisterActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        auth = Firebase.auth

        setContentView(R.layout.activity_register)
        var signinlinkbtn = findViewById<Button>(R.id.signin_link_btn)

        signinlinkbtn.setOnClickListener { startActivity(Intent(this, LoginActivity::class.java)) }

        val registerButton = findViewById<Button>(R.id.signup_btn)
        registerButton.setOnClickListener { performSignUp() }
    }

    private fun performSignUp() {
        val email = findViewById<EditText>(R.id.email_register)
        val password = findViewById<EditText>(R.id.password_register)

        if (email.text.isEmpty() || password.text.isEmpty()){
            Toast.makeText(this, "请填写所有字段!", Toast.LENGTH_SHORT).show()
            return
        }

        val inputEmail = email.text.toString()
        val inputPassword = password.text.toString()

        auth.createUserWithEmailAndPassword(inputEmail, inputPassword)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // 注册成功,跳转到主活动
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)

                    Toast.makeText(
                        baseContext,
                        "注册成功",
                        Toast.LENGTH_SHORT,
                    ).show()

                } else {
                    // 如果注册失败,向用户显示消息
                    Toast.makeText(
                        baseContext,
                        "身份验证失败",
                        Toast.LENGTH_SHORT,
                    ).show()
                }
            }
            .addOnFailureListener {
                Toast.makeText(this, "发生错误", Toast.LENGTH_LONG).show()
            }
    }
}

如果有人知道问题是什么,请告诉我。

英文:

When I put the information in the textfields the button just doesn't react when I click it. The data goes to Firebase but it doesn't affect how the app works. My Toast message is working if the textfields are empty(the button reacts), but when they are full nothing on screen happens. When signing up or in, the screen should go to Main Activity but nothings happens. Here is my code:

LoginActivity

class LoginActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
       auth = Firebase.auth


        var signuplinkbtn = findViewById&lt;Button&gt;(R.id.signup_link_btn)

        signuplinkbtn.setOnClickListener {
            startActivity(
                Intent(
                    this,
                    RegisterActivity::class.java
                )
            )
        }

        val loginButton = findViewById&lt;Button&gt;(R.id.login_btn)

        loginButton.setOnClickListener {
            performLogin()
        }

    }

    private fun performLogin() {
        val email = findViewById&lt;EditText&gt;(R.id.email_login)
        val password = findViewById&lt;EditText&gt;(R.id.password_login)

        if (email.text.isEmpty() || password.text.isEmpty()){
            Toast.makeText(this, &quot;Please, fill all fields&quot;, Toast.LENGTH_LONG).show()
            return
        }
        val emailInput = email.text.toString()
        val passwordInput = password.text.toString()

        auth.createUserWithEmailAndPassword(emailInput, passwordInput)
            .addOnCompleteListener(this) { task -&gt;
                if (task.isSuccessful) {
                    // Sign in success, move to Main Activity
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)

                    Toast.makeText(
                        baseContext,
                        &quot;Success&quot;,
                        Toast.LENGTH_SHORT,
                    ).show()

                } else {
                    // If sign in fails, display a message to the user.

                    Toast.makeText(
                        baseContext,
                        &quot;Authentication failed.&quot;,
                        Toast.LENGTH_SHORT,
                    ).show()
                }
            }
            .addOnFailureListener {
                Toast.makeText(this, &quot;Error occured&quot;, Toast.LENGTH_LONG).show()
            }
    }
}

RegisterActicity:

class RegisterActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        auth = Firebase.auth

        setContentView(R.layout.activity_register)
        var signinlinkbtn = findViewById&lt;Button&gt;(R.id.signin_link_btn)

        signinlinkbtn.setOnClickListener { startActivity(Intent(this, LoginActivity::class.java)) }

        val registerButton = findViewById&lt;Button&gt;(R.id.signup_btn)
        registerButton.setOnClickListener { performSignUp() }

    }

    private fun performSignUp() {
        val email = findViewById&lt;EditText&gt;(R.id.email_register)
        val password = findViewById&lt;EditText&gt;(R.id.password_register)

        if (email.text.isEmpty() || password.text.isEmpty()){
            Toast.makeText(this, &quot;Please, fill all the fields!&quot;, Toast.LENGTH_SHORT).show()
            return
        }

        val inputEmail = email.text.toString()
        val inputPassword = password.text.toString()

        auth.createUserWithEmailAndPassword(inputEmail, inputPassword)
            .addOnCompleteListener(this) { task -&gt;
                if (task.isSuccessful) {
                    // Sign in success, move to Main Activity
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)

                    Toast.makeText(
                        baseContext,
                        &quot;Success&quot;,
                        Toast.LENGTH_SHORT,
                    ).show()

                } else {
                    // If sign in fails, display a message to the user.

                    Toast.makeText(
                        baseContext,
                        &quot;Authentication failed.&quot;,
                        Toast.LENGTH_SHORT,
                    ).show()
                }
            }
            .addOnFailureListener {
                Toast.makeText(this, &quot;Error occured&quot;, Toast.LENGTH_LONG).show()
            }
    }
}

If someone has any idea what the problem is, I'd be glad to know.

答案1

得分: 1

我认为你漏掉了一些东西;要使用Firebase Auth进行登录,你需要使用 signInWithEmailAndPassword 而不是 createUserWithEmailAndPassword,因为后者用于首次创建用户,这就是为什么它不起作用的原因。

请按以下方式修改你的代码在 LoginActivity 中:

private fun performLogin() {
    val email = findViewById<EditText>(R.id.email_login)
    val password = findViewById<EditText>(R.id.password_login)

    if (email.text.isEmpty() || password.text.isEmpty()) {
        Toast.makeText(this, "请填写所有字段", Toast.LENGTH_LONG).show()
        return
    }
    
    val emailInput = email.text.toString()
    val passwordInput = password.text.toString()

    auth.signInWithEmailAndPassword(emailInput, passwordInput)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // 登录成功,跳转到MainActivity
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                Toast.makeText(baseContext, "登录成功", Toast.LENGTH_SHORT).show()
            } else {
                // 如果登录失败,向用户显示消息
                Toast.makeText(baseContext, "身份验证失败", Toast.LENGTH_SHORT).show()
            }
        }
        .addOnFailureListener {
            Toast.makeText(this, "发生错误", Toast.LENGTH_LONG).show()
        }
}

希望这能解决问题 🙏

英文:

I think you are missing something; in order to log in with Firebase Auth, you need to use signInWithEmailAndPassword and not createUserWithEmailAndPassword, because the latter is used to create users for the first time, that's why it is not working I think.

Modify your code as follows In LoginActivity:

private fun performLogin() {
    val email = findViewById&lt;EditText&gt;(R.id.email_login)
    val password = findViewById&lt;EditText&gt;(R.id.password_login)

    if (email.text.isEmpty() || password.text.isEmpty()) {
        Toast.makeText(this, &quot;Please fill all fields&quot;, Toast.LENGTH_LONG).show()
        return
    }
    
    val emailInput = email.text.toString()
    val passwordInput = password.text.toString()

    auth.signInWithEmailAndPassword(emailInput, passwordInput)
        .addOnCompleteListener(this) { task -&gt;
            if (task.isSuccessful) {
                // Sign in success, move to MainActivity
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                Toast.makeText(baseContext, &quot;Login success&quot;, Toast.LENGTH_SHORT).show()
            } else {
                // If sign in fails, display a message to the user.
                Toast.makeText(baseContext, &quot;Authentication failed.&quot;, Toast.LENGTH_SHORT).show()
            }
        }
        .addOnFailureListener {
            Toast.makeText(this, &quot;Error occurred&quot;, Toast.LENGTH_LONG).show()
        }
}

Hope that will solve the issue 🙏

huangapple
  • 本文由 发表于 2023年5月29日 13:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定