You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin中从Firebase登录邮箱提取用户名并展示

实现登录后提取邮箱前缀并展示欢迎语

一、提取邮箱前缀的两种逻辑

根据需求提供两种提取方式,可按需选择:

  • 提取@符号前的完整内容(如user2.check@gmail.com提取user2.check)
  • 提取@符号前第一个.之前的内容(如user2.check@gmail.com提取user2)

对应的Kotlin工具函数:

// 提取@前完整内容
fun extractFullPrefix(email: String): String {
    return email.substringBefore("@")
}

// 提取@前第一个.之前的内容
fun extractShortPrefix(email: String): String {
    val prefix = email.substringBefore("@")
    return prefix.substringBefore(".")
}

二、修改登录逻辑传递用户名前缀

在MainActivity的登录成功回调中,获取当前用户邮箱并处理,通过Intent传递到Dashboard页面:

修改MainActivity中的login方法及添加提取函数:

class MainActivity : AppCompatActivity() {
    // ... 原有代码保持不变

    private fun login() {
        val userName = username.text.toString()
        val pass = password.text.toString()

        if (userName.isNotEmpty() && pass.isNotEmpty()) {
            auth.signInWithEmailAndPassword(userName, pass).addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // 获取当前用户邮箱,处理为空的情况
                    val userEmail = auth.currentUser?.email ?: ""
                    // 选择提取方式,这里以完整前缀为例
                    val userPrefix = extractFullPrefix(userEmail)
                    
                    val intent = Intent(this, Dashboard::class.java)
                    intent.putExtra("USER_PREFIX", userPrefix)
                    startActivity(intent)
                    Toast.makeText(this, "Successfully LoggedIn", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "Log In failed ", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    private fun extractFullPrefix(email: String): String {
        return email.substringBefore("@")
    }

    private fun extractShortPrefix(email: String): String {
        val prefix = email.substringBefore("@")
        return prefix.substringBefore(".")
    }
}

三、Dashboard页面展示欢迎语

创建或修改Dashboard Activity,添加TextView用于展示欢迎语,并接收传递的用户名前缀:

Dashboard Activity代码:

class Dashboard : AppCompatActivity() {
    private lateinit var welcomeText: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dashboard)

        welcomeText = findViewById(R.id.tv_welcome)
        
        // 获取传递的前缀,默认显示Guest
        val userPrefix = intent.getStringExtra("USER_PREFIX") ?: "Guest"
        welcomeText.text = "Welcome $userPrefix"
    }
}

对应的activity_dashboard.xml布局示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textStyle="bold"/>

</LinearLayout>

备选方案:直接在Dashboard获取用户邮箱

如果不想通过Intent传递数据,可直接在Dashboard中从Firebase Auth获取当前用户邮箱并处理:

class Dashboard : AppCompatActivity() {
    private lateinit var welcomeText: TextView
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dashboard)

        welcomeText = findViewById(R.id.tv_welcome)
        auth = FirebaseAuth.getInstance()
        
        val userEmail = auth.currentUser?.email ?: ""
        val userPrefix = extractFullPrefix(userEmail)
        welcomeText.text = "Welcome $userPrefix"
    }

    private fun extractFullPrefix(email: String): String {
        return email.substringBefore("@")
    }
}

内容的提问来源于stack exchange,提问作者Akshay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:17:44