如何使用kotlin在另一页的文本视图中显示toast.maketext

发布于 2025-01-19 21:07:47 字数 994 浏览 5 评论 0 原文

//这是我使用kotlin覆盖的扫描仪条形码代码

fun接收访问(检测:detector..detections){

            val barcodes = detections.detectedItems
            if (barcodes.size() == 1) {
                scannedValue = barcodes.valueAt(0).rawValue
                runOnUiThread {
                    cameraSource.stop()
                    Toast.makeText(this@InsertStockInActivity, "value- $scannedValue", Toast.LENGTH_SHORT).show()
                    finish()
                }
            }else
            {
                Toast.makeText(this@InsertStockInActivity, "value- else", Toast.LENGTH_SHORT).show()

            }
        }

//这是我的输入页面

覆盖fun increateview(fordater:layoutinflater,container,container:viewgroup? {

    binding = FragmentInputStockInBinding.inflate(inflater, container, false)
    binding.btnScanBarcode.setOnClickListener{ nav.navigate(R.id.insertStockInActivity)}

    return binding.root
}[enter image description here][1]

//This is my scanner barcode code using kotlin

override fun receiveDetections(detections: Detector.Detections) {

            val barcodes = detections.detectedItems
            if (barcodes.size() == 1) {
                scannedValue = barcodes.valueAt(0).rawValue
                runOnUiThread {
                    cameraSource.stop()
                    Toast.makeText(this@InsertStockInActivity, "value- $scannedValue", Toast.LENGTH_SHORT).show()
                    finish()
                }
            }else
            {
                Toast.makeText(this@InsertStockInActivity, "value- else", Toast.LENGTH_SHORT).show()

            }
        }

//This is my input page

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    binding = FragmentInputStockInBinding.inflate(inflater, container, false)
    binding.btnScanBarcode.setOnClickListener{ nav.navigate(R.id.insertStockInActivity)}

    return binding.root
}[enter image description here][1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2025-01-26 21:07:47

我想您应该将吐司文本提取为变量,然后将其传递到另一页(我认为您的均值是另一个片段/活动或另一个屏幕)


  private lateinit var toastText:String
  ...

  if (barcodes.size() == 1) {
    ...     
    toastText = scannedValue      
    ...
  } else {
    toastText = "value- else"
  }

  Toast.makeText(this@InsertStockInActivity, toastText , Toast.LENGTH_SHORT).show()

}

,然后通过 toastText 传递到另一个页面>意图或 Safe-args 如果您使用的是JetPack Navigation

i thnk you should extract your toast text as a variable and pass it to another page ( i assume that your mean are to another fragment/activity or to another screen)


  private lateinit var toastText:String
  ...

  if (barcodes.size() == 1) {
    ...     
    toastText = scannedValue      
    ...
  } else {
    toastText = "value- else"
  }

  Toast.makeText(this@InsertStockInActivity, toastText , Toast.LENGTH_SHORT).show()

}

and pass toastText to another page via Intent or safe-args if you are using Jetpack Navigation

蓝眼泪 2025-01-26 21:07:47

您可以使用视图模型,实时数据或另一个(静态)对象将结果保存在变量中。按照相同的行,您可以在另一个类中创建一个show吐司功能,只需传递您所处的片段或活动的上下文即可。

 fun showToast(context: Context?, message: String) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}

You could use a view model, live data, or another (static) object to hold your results in a variable. Along the same lines, you can create a show toast function in another class and just pass the context of the fragment or activity that you are in. For example a fragment context could be requireContext().

 fun showToast(context: Context?, message: String) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文