Copy text from TextView on Android

前端 未结 6 1049
野趣味
野趣味 2020-12-02 21:10

I have a ListView where each item is a TextView.

I want to enable the long press behaviour similar to an EditText that display

6条回答
  •  Happy的楠姐
    2020-12-02 22:00

    Here is the solution

     
    

    override setOnLongClickListener

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textID.setTextIsSelectable(true)
        textID. setOnLongClickListener {
           val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
           val clip = ClipData.newPlainText("Copied String",  textID.text)
           clipboardManager.setPrimaryClip(clip)
           true // Or false if not consumed
       }
    }
    

    the expected behavior will be like the image below

提交回复
热议问题