Skip to content

Instantly share code, notes, and snippets.

@hyoban
Created January 23, 2021 12:02
Show Gist options
  • Save hyoban/ec809ff08ee84077dcba9e9272a51bbe to your computer and use it in GitHub Desktop.
Save hyoban/ec809ff08ee84077dcba9e9272a51bbe to your computer and use it in GitHub Desktop.
添加文本选择菜单
customSelectionActionModeCallback = object : ActionMode.Callback2() {
override fun onCreateActionMode(actionMode: ActionMode, menu: Menu): Boolean {
val menuInflater = actionMode.menuInflater
menuInflater.inflate(R.menu.text_select_menu, menu)
return true //返回false则不会显示弹窗
}
override fun onPrepareActionMode(actionMode: ActionMode, menu: Menu): Boolean {
return false
}
override fun onActionItemClicked(
actionMode: ActionMode,
menuItem: MenuItem
): Boolean {
val textStart = selectionStart
val textEnd = selectionEnd
//根据item的ID处理点击事件
when (menuItem.itemId) {
R.id.menu_search_word -> {
text.subSequence(textStart, textEnd).toString().showToast()
actionMode.finish() //收起操作菜单
}
}
return false //返回true则系统的"复制"、"搜索"之类的item将无效,只有自定义item有响应
}
override fun onDestroyActionMode(actionMode: ActionMode) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment