【问题标题】:I get a "Val cannot be reasigned error" even though I assigned the variable as var?即使我将变量分配为 var,我也会收到“Val 无法重新签名错误”?
【发布时间】:2021-09-04 13:00:02
【问题描述】:

我正在尝试向应用程序添加“复制到剪贴板”功能。我在图像按钮上添加了一个 setOnClickListener 用于复制并编写了下面的代码。我仍然在“clipBoard.primaryClip = clip”行上收到“val cannot be reassigned”错误

            var clipBoard: ClipboardManager = getActivity()?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val StrEdtFirtname = etNoteDesc.text.toString()
            val clip = ClipData.newPlainText("Copied text",StrEdtFirtname)
            clipBoard.primaryClip = clip
            Toast.makeText(requireContext(), "Copied text", Toast.LENGTH_SHORT).show()
        }

【问题讨论】:

    标签: android kotlin clipboardmanager


    【解决方案1】:

    试试下面的代码。

    var clipBoard: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
         val StrEdtFirtname = etNoteDesc.text.toString()
        val clip = ClipData.newPlainText("Copied text",StrEdtFirtname)
        clipBoard.apply {
            setPrimaryClip(clip)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多