Android long press pop-up menu, copy, paste, select all, select all for android

Source: Internet
Author: User

Android long press pop-up menu, copy, paste, select all, select all for android

<! -- Define basic layout LinearLayout --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/activity_horizontal_margin" android: paddin GTop = "@ dimen/activity_vertical_margin" android: orientation = "vertical"> <! -- Define the TextView control --> <EditText android: id = "@ + id/Et" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Long press pop-up content menu"/> </LinearLayout>
Package com. example. yanlei. yl2; import android. content. clipData; import android. content. clipboardManager; import android. content. context; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. util. log; import android. view. contextMenu; import android. view. contextMenu. contextMenuInfo; import android. view. menu; import android. view. menuItem; import android. view. view; import android Oid. widget. editText; import android. widget. toast; // import the required package public class MainActivity extends AppCompatActivity {// define the Id final int Menu_1 = Menu of each Menu option in ContextMenu. FIRST; final int Menu_2 = Menu. FIRST + 1; final int Menu_3 = Menu. FIRST + 2; EditText Et; private ClipboardManager mClipboard = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // call the parent class OnCreate method // set the layout file of the current page to activity_main setContentView (R. layout. activity_main); // obtain the layout control Et = (EditText) findViewById (R. id. et); // register the context menu registerForContextMenu (Et) for EditText;} // create the callback method of the ContextMenu menu public void onCreateContextMenu (ContextMenu m, View v, ContextMenuInfo menuInfo) {super. onCreateContextMenu (m, v, menuInfo); // add option content to context menu options // add method parameters: add (group id, itemid, sorting, menu text) M. add (0, Menu_1, 0, "Copy text"); m. add (0, Menu_2, 0, "paste text"); m. add (0, Menu_3, 0, "select all text");} private void copyFromEditText1 () {// Gets a handle to the clipboard service. if (null = mClipboard) {mClipboard = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE);} // Creates a new text clip to put on the clipboard ClipData clip = ClipData. newPlainText ("simple text", Et. getText (); // Set Clipboard's primary clip. mClipboard. setPrimaryClip (clip);} private void pasteToResult () {// Gets a handle to the clipboard service. if (null = mClipboard) {mClipboard = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE);} String resultString = ""; // check whether the Clipboard contains content if (! MClipboard. hasPrimaryClip () {Toast. makeText (MainActivity. this, "Clipboard is empty", Toast. LENGTH_SHORT ). show ();} else {ClipData clipData = mClipboard. getPrimaryClip (); int count = clipData. getItemCount (); for (int I = 0; I <count; ++ I) {ClipData. item item = clipData. getItemAt (I); CharSequence str = item. coerceToText (MainActivity. this); Log. I ("mengdd", "item:" + I + ":" + str); resultString + = str ;}} Toast. makeText (this, resultString, Toast. LENGTH_SHORT ). show (); Et. setText (resultString);} // The callback event public boolean onContextItemSelected (MenuItem item) selected by the option in the ContextMenu menu) {// The parameter is the selected menu option object // execute the corresponding function switch (item. getItemId () {case 1: Toast. makeText (this, "Copy text", Toast. LENGTH_SHORT ). show (); copyFromEditText1 (); break; case 2: Toast. makeText (this, "paste text", Toast. LENGTH_SHORT ). show (); pasteToResult (); break; case 3: Toast. makeText (this, "select all text", Toast. LENGTH_SHORT ). show (); Et. selectAll (); break;} return super. onOptionsItemSelected (item );}}

 

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.