LoginSignup
0
0

More than 1 year has passed since last update.

Android 指定した文字をクリップボードにコピーする

Posted at

今回は指定した文字をクリップボードにコピーする方法です。

ボタンを押した後に、特定の文字をコピーさせたい。みたいな場合に使用することができます。

MainActivity.kt
    /**
     * クリップボードにコピーする。
     *
     * @param context     コンテキスト
     * @param copyText     コピーする文字列
     */
    private fun copyClipboard(context: Context, copyText: String?) {
        val clipboardManager: ClipboardManager = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        clipboardManager.setPrimaryClip(ClipData.newPlainText("", copyText))
    }

こちらでcopyTextに渡した文字がコピーされます。

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0