How can I write my own context menu for copy & paste?

∥☆過路亽.° 提交于 2019-12-08 23:58:47

问题


I'm writing an application with EditText driven widget. and I'd like to create my own copy & paste menu. To replace android default menu on EditText, what should I do? Just overriding long click? or is there another way to implement? Thanks in advance.


回答1:


EditText should already have a context menu enabled. If it were not, then you would have to enable it by calling registerForContextMenu. Once you have the context menu enabled, you have to add items to it. This is done in onCreateContextMenu by using one of the Menu.add methods.

The hard part is writing the code for onContextItemSelected after the user has selected an option. Saving text to the clipboard is simply a matter of calling ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).setText("myText");. However, first we need to find what text to copy. I haven't figured this last part out yet, but I am hopeful that I will soon.

Related Questions

  • Show context menu when link is long pressed in TextView



回答2:


It is considered to be somewhat of a standard exercise to implement copy/paste the hard way by overwriting the menu system, creating the menu items yourself, and your own internal buffer.

However, that is not how it should be done if a better way is available on the platform. Reimplementing platform functions is good for learning but bad for maintenance.

Community Wiki as this is not a real answer and I should not get rep for this.



来源:https://stackoverflow.com/questions/2226836/how-can-i-write-my-own-context-menu-for-copy-paste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!