如何运行 两个函数对AlertDialog Positive button click?

How to run two functions on AlertDialog Positive button click?

我是 android 的新手,我在我的项目中创建了一个基本的警报对话框,它只有 [一条消息和一个肯定按钮和一个否定按钮] 该消息是一个问题,询问 [做你想退出帐户吗?] 然后按下否定按钮关闭警报对话框,按下肯定按钮它也会关闭警报对话框并且什么都不做。我想关闭 NEGATIVE BUTTON 就像关闭警报对话框一样,但是当我按下 POSITIVE BUTTON 时,它为我做了两个工作 [1- 从应用程序注销和 2- 启动我的 activity,它被称为 LoginActivity.kt]

注意:我已经通过另一个 activity_sign_out.xml 布局完成了这两项工作,其中包含用于完成这两项工作的按钮。但是我想在显示 alertdialog 时通过按正按钮来完成这两项工作。

希望你能清楚我的问题。

我试着做到了,但它不起作用,这是我的代码:

1- 我的警报对话框:

import android.content.DialogInterface
import android.content.Intent
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import com.google.firebase.auth.FirebaseAuth
import java.util.*

class DashboardActivity : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    loadLocale()
    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 navView = findViewById<NavigationView>(R.id.nav_view)
    val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close)
    toggle.isDrawerIndicatorEnabled = true
    supportActionBar?.setDisplayShowTitleEnabled(false)
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
    navView.setNavigationItemSelectedListener {
        when(it.itemId){
            R.id.itmSignOut ->startActivity(Intent(this,SignOut::class.java))
            R.id.itmLanguage ->showChangeLanguageDialog()
            R.id.itmProfile ->basicAlert()
        }
        true
    }
}
private fun basicAlert(){
    val positiveButtonClick = { _: DialogInterface, _: Int -> }
    val negativeButtonClick = { _: DialogInterface, _: Int -> }
    val builder = AlertDialog.Builder(this)
    with(builder) {
        setMessage(R.string.do_you_want_to_sign_out_from_your_my_conquer_account)
        setPositiveButton(R.string.yes, ({ _: DialogInterface, _: Int ->
            positiveButtonClick
            auth.signOut()
            startActivity(Intent(this,LoginActivity::class.java))
            finish()
        }))
        setNegativeButton(R.string.no, negativeButtonClick)
        show()
    }
}

2- 我的 SignOut.kt :

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()
}
}

3- 和 activity_sign_out.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SignOut">

    <TextView
        android:id="@+id/tvUserEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome"
        android:textAlignment="center"
        android:textSize="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnSignOut"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:background="@drawable/custom_button"
        android:text="@string/sign_out"
        android:textAllCaps="false"
        android:textColor="@color/White"
        android:textSize="18dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.8"
        tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>

最后在 运行 应用程序和修复它的代码之后,我添加了 auth.signOut() startActivity(Intent(this,LoginActivity::class.java)) finish() 到我的 Alertdialog,但它显示红色 Intent 的错误:[None 可以使用提供的参数调用以下函数。

谢谢你帮助我!保重。

如您在 Android Studio 中所见,with 块中的 this 指的是 AlertDialog.Builder 实例。要引用 Activity 你必须明确地说:

setPositiveButton(R.string.yes) { _: DialogInterface, _: Int ->
    auth.signOut()
    startActivity(Intent(this@DashboardActivity, LoginActivity::class.java))
    finish()
}