【问题标题】:How do I send a string from dialogFragment to a Activity?如何将字符串从 dialogFragment 发送到 Activity?
【发布时间】:2022-01-07 19:54:42
【问题描述】:

我想从 dialogFragment 弹出窗口发送一个 editText 字符串到它后面的活动屏幕。我不确定如何在 Kotlin 中执行此操作,因为大多数在线资源都使用 Java。我试图实现:How to send data from dialog to my activity kotlin?。但是,我不能完全让它工作,所以我不确定从这里使用什么方法。

这是 SelectAPScreen.kt(我希望字符串转到的活动)

package com.wcsng.dlocapp

import android.annotation.SuppressLint
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.findNavController
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.imu_data.*
import java.io.BufferedReader
import java.io.InputStreamReader

class SelectAPScreen : AppCompatActivity(), OnButtonClick {


    @SuppressLint("ResourceAsColor")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_apscreen)

        val directoryName = findViewById<EditText>(R.id.dir_field)
        val pingIP = findViewById<EditText>(R.id.ip_field)
        val rpiIP = findViewById<EditText>(R.id.rpiField)
        val qtnIPs = {}

        val addQtn = findViewById<Button>(R.id.addQtn)
        addQtn.setOnClickListener {
            var dialog = AddQtnFragment()
            dialog.show(supportFragmentManager, "Add Qtn Popup")
        }
        
    }
}


这是 DialogFragment 代码:

package com.wcsng.dlocapp

import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.findNavController
import com.wcsng.dlocapp.R

class AddQtnFragment: DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            // Get the layout inflater
            val inflater = requireActivity().layoutInflater

            val qtn_ip = view?.findViewById<EditText>(R.id.qtn_ip)?.text.toString()


            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.add_qtn_popup, null))
                .setPositiveButton("Save", DialogInterface.OnClickListener{dialog, id ->
                    // TODO Return the qtn_ip to SelectAPScreen

                    // Close the dialog and return back to the parent activity
                    dialog.dismiss()
                })
                .setNegativeButton("Cancel", DialogInterface.OnClickListener{dialog, id ->
                    dialog.dismiss()
                })
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }


}

【问题讨论】:

    标签: android android-studio kotlin


    【解决方案1】:

    您可以使用getActivity()requireActivity() 方法来获取管理Activity,然后您可以将其转换为适当的类,例如(requireActivity() as SelectAPScreen) 现在你可以调用它的方法了

    (requireActivity() as SelectAPScreen).selectAPScreenMethod()
    

    selectAPScreenMethod() 是放在SelectAPScreen 类中的自定义方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多