[Android] TextView implementation method summary (reproduced) by long-pressed copy, androidtextview
This is written by others. Since it has been summarized by others, I will not spend any time studying this. However, I will add some useful experience in the future.
Address: http://blog.csdn.net/stzy00/article/details/41477813
Address: http://www.cnblogs.com/rossoneri/p/4432499.html
To meet this requirement, You need to press the text to display the copy option to save the text or use the information on other pages. similarly, the copy option is automatically displayed when you press WebView or EditText. there are two main features: 1. Users can only browse text information but cannot edit the text information; 2, users can copy text information by clicking the "copy" option that can pop up for a long time. There are many ways to achieve this on the Internet, which are also scattered. Here is a summary, hoping to help.
Customize TextView by inheriting EditText
Public class NewTextView extends EditText {public NewTextView (Context context) {super (context); // TODO Auto-generated constructor stub} public NewTextView (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stub} public NewTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // TODO Auto-generated constructor stub} @ Override protected boolean getDefaultEditable () {// disable EditText from being edited return false ;}}
The key method above is getDefaultEditable (), return false; that is, the EditText cannot be edited and becomes an EditText that cannot be edited, but it also has the long-press copy function of EditText. further, if you look at the EditText source code, you will find that there is a method in the source code.Protected void onCreateContextMenu (ContextMenu menu)Controls the context menu popped up by the EditText long press. You can customize this method and implement it as an empty protected void onCreateContextMenu (ContextMenu menu ){}. then, in the onTouchEvent event, intercept and process custom point-by-Event Events. A custom menu is displayed. the principle is as follows. A simpler method is to directly use an EditText in xml and set the property to android: editable = "false ".
Use OnLongClickListener
Use TextView directly, and then add the OnLongClickListener event to the TextView in the Code. The custom "copy" menu is displayed on time (for example, the pop-up menu is made with PopupWindow ). click "copy" to get the TextView content.
Of course, here we only get the content. We also need a ClipboardManager object for how to put the content into the paste manager. It manages copying and pasting.
ClipboardManager cmb = (ClipboardManager) context. getSystemService (Context. CLIPBOARD_SERVICE); cmb. setText (content. trim (); // Add the content to the paste manager, and press and select Paste elsewhere. getText (); // obtain the paste information
Use setTextIsSelectable () method
The Code directly uses the setTextIsSelectable () method for TextView and sets TextView as selectable.
TextView tv = new TextView(context); tv.setTextIsSelectable(true);
The above is a summary of some common methods to pop up the "copy" menu with long-pressed text information. I hope it will be helpful to you.