There is a need for users to browse through the text message and want to press and hold the information to pop up the copied options to save or use the information on other pages. Similarly, the Copy option pops up automatically as long as you press WebView or edittext content.
Here are 2 main features:
1, the user can only browse the text information and cannot edit the text information;
2, the user to the text information for a long time click can pop up the "copy" option to achieve replication;
There are many ways to achieve online, but also relatively fragmented, here to do a summary, hope to help.
1, by inheriting EditText, custom TextView
| 123456789101112131415161718192021 |
publicclassNewTextView extendsEditText { publicNewTextView(Context context) { super(context); // TODO Auto-generated constructor stub } publicNewTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } publicNewTextView(Context context, AttributeSet attrs, intdefStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protectedbooleangetDefaultEditable() {//禁止EditText被编辑 returnfalse; }} |
One of the key methods above is getdefaulteditable (), return false, which prohibits edittext from being edited into a non-editable edittext, but also has the EditText long-press copy function. Further, if the view EditText source code, found that there is a method in the source code protected void Oncreatecontextmenu (ContextMenu menu)
Control EditText Long Press the popup context menu, you can customize this method and implement the null protected void Oncreatecontextmenu (ContextMenu menu) {}
The Ontouchevent event is then intercepted to handle a custom point-by-event, bouncing out the defined menu.
The more straightforward way to do this is to use a edittext directly in the XML and set the property to Android:editable= "false".
2, using Onlongclicklistener
Use TextView directly, and then add Onlongclicklistener events to TextView in your code, and long-time bounce out of the definition "copy" menu (such as combining popupwindow for pop-up menus). Click "Copy" to get the content of TextView.
Of course, this is just to get the content, how to put the content into the Paste manager also requires a Clipboardmanager object. It is responsible for managing the post-copy paste.
| 123 |
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);cmb.setText(content.trim()); //将内容放入粘贴管理器,在别的地方长按选择"粘贴"即可cm.getText();//获取粘贴信息 |
3, using the Settextisselectable () method
Use the Settextisselectable () method directly on TextView in your code to set TextView to tap the selection.
| 12 |
TextView tv = newTextView(context);tv.setTextIsSelectable(true); |
Above is a common long-press text message pop up the "copy" menu some of the methods summarized, I hope to help you.
Reprint Please specify: Android Development Chinese station»textview long Press Copy implementation method summary
Original link: http://www.androidchina.net/1258.html
"Go" TextView long press Copy implementation method summary