Skip to content

Instantly share code, notes, and snippets.

@robsenshuu
Created October 9, 2019 23:10
Show Gist options
  • Save robsenshuu/a772b55035ef79e1f6713ac18c0c92ed to your computer and use it in GitHub Desktop.
Save robsenshuu/a772b55035ef79e1f6713ac18c0c92ed to your computer and use it in GitHub Desktop.
class CutCopyPasteEditText @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : EditText(context, attrs, defStyleAttr) {
interface OnCutCopyPasteListener {
fun onCut()
fun onCopy()
fun onPaste()
}
private var cutCopyPasteListener: OnCutCopyPasteListener? = null
fun setOnCutCopyPasteListener(listener: OnCutCopyPasteListener) {
cutCopyPasteListener = listener
}
override fun onTextContextMenuItem(id: Int): Boolean {
val consumed = super.onTextContextMenuItem(id)
when(id) {
android.R.id.cut -> {
cutCopyPasteListener?.onCut()
}
android.R.id.copy -> {
cutCopyPasteListener?.onCopy()
}
android.R.id.paste -> {
cutCopyPasteListener?.onPaste()
}
}
return consumed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment