Android copy, paste, and cut Functions

Source: Internet
Author: User

There are a lot of copy and paste articles on the Internet, but they are stored in their own programs and do not know how to handle them. Now we can find a feasible method as follows:
Android clipboard (ClipboardManager)
Note: When importing a package
Before API 11: android. text. ClipboardManager
After API 11: android. content. ClipboardManager Copy codeThe Code is as follows :/**
* Implement text Replication
* @ Param content
*/
Public static void copy (String content, Context context ){
// Obtain the clipboard manager.
ClipboardManager cmb = (ClipboardManager) context
. GetSystemService (Context. CLIPBOARD_SERVICE );
Cmb. setText (content. trim ());
}

Copy codeThe Code is as follows :/**
* Implement the paste Function
*
* @ Param context
* @ Return
*/
Public static String paste (Context context ){
// Obtain the clipboard manager.
ClipboardManager cmb = (ClipboardManager) context
. GetSystemService (Context. CLIPBOARD_SERVICE );
Return cmb. getText (). toString (). trim ();
}

Many articles on the Internet are written in this way:
First: Copy codeThe Code is as follows: private void emulateShiftHeld (KeyEvent. Callback view ){
Try {
KeyEvent shiftPressEvent = new KeyEvent (0, 0, KeyEvent. ACTION_DOWN,
KeyEvent. KEYCODE_SHIFT_LEFT, 0, 0 );
ShiftPressEvent. dispatch (view );
} Catch (Exception e ){
}
}

Second:
Versions later than android2.1
1: (succeeded) Copy codeThe Code is as follows: ClipboardManager clip = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );
Clip. getText (); // Paste
Clip. setText (str); // copy

Second: Copy codeThe Code is as follows: ClipboardManager c = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE );
C. setText (smsContent. getText (); // set the content of the Clipboard
C. getText (smsContent. getText (); // extract the content of the clipboard

Versions earlier than android2.1Copy codeThe Code is as follows: IClipboard clip = IClipboard. Stub. asInterface (ServiceManager. getService ("clipboard "));
Clip. getClipboardText (). toString (); // obtain the copied content.
Clip. setClipboardText (text); // set the content of the Clipboard

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.