如何通过从导航抽屉的菜单中选择一个项目来打开 activity? [科特林]

How to open an activity by selecting an item from navigation drawer's menu? [KOTLIN]

我是 kotlin 和 android studio 的新手,但作为初学者,我完成了第一个项目的 40%。 我想通过从导航抽屉菜单中选择一个项目来打开 activity,但尝试使用其他答案并不能解决我的问题,我看到的类似问题没有明确的答案,所有这些都在 java 语言,我想在我的项目中使用 KOTLIN 语言来实现。我希望你们能清楚我的问题!

这是我所有的项目相关代码:

1- 这是我的导航抽屉菜单项:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group
android:checkableBehavior="single">
<item
android:id="@+id/itmSignOut"
android:title="@string/sign_out"/>
</group>
</menu>

2- 这是导航抽屉所在的仪表板:

import af.azdreams.myconquer.databinding.ActivityDashboardBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout

class DashboardActivity : AppCompatActivity() {
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityDashboardBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)
    val drawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
    val toolbar = findViewById<Toolbar>(R.id.toolbar)
    val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close)
    toggle.isDrawerIndicatorEnabled = true
    supportActionBar?.setDisplayShowTitleEnabled(false)
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
}
private var backPressedTime:Long = 0
private lateinit var backToast: Toast
override fun onBackPressed() {
    backToast = 
Toast.makeText(this,resources.getString(R.string.press_back_again_to_leave_the_app), 
Toast.LENGTH_SHORT)
    if (backPressedTime + 2000 > System.currentTimeMillis()) {
        backToast.cancel()
        super.onBackPressed()
        return
    } else {
        backToast.show()
    }
    backPressedTime = System.currentTimeMillis()
}
}

3- 这是我要在从项目菜单中选择后打开的 activity :

import af.azdreams.myconquer.databinding.ActivitySignOutBinding
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.auth.FirebaseAuth

class SignOut : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivitySignOutBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivitySignOutBinding.inflate(layoutInflater)
    setContentView(R.layout.activity_sign_out)
    val view = binding.root
    setContentView(view)
    initData()
}
private fun initData(){
    auth = FirebaseAuth.getInstance()
    setUserEmail()
    clickListener()
}
private fun clickListener(){
    binding.btnSignOut.setOnClickListener{
        auth.signOut()
        startActivity(Intent(this,LoginActivity::class.java))
        finish()
    }
}
private fun getCurrentUserEmail():String? {
    return auth.currentUser?.email
}
@SuppressLint("SetTextI18n")
private fun setUserEmail(){
    binding.tvUserEmail.text = "Welcome "+getCurrentUserEmail()
}
}

4- 这是 dashboard.xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/Orange"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
    app:menu="@menu/nav_menu"
    android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

谢谢你帮我解决这个问题。 保重!

您要查找的内容称为 Intent: https://developer.android.com/reference/kotlin/android/content/Intent

你的情况 您必须在第一个 activity 中覆盖 onOptionsItemSelected 方法,然后您可以创建一个用于导航到第二个 activity.

的意图

像这样

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when(item.itemId) {
        val intent = Intent(this,SecondActivity::class.java).apply
        startActivity(intent) 
        return true
    }
    return super.onOptionsItemSelected(item)
}