开发者

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

开发者 https://www.devze.com 2022-12-19 19:44 出处:网络
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?

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.开发者_StackOverflow


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消