How do I enable standard copy paste for a TextView in Android?

后端 未结 9 1832
旧时难觅i
旧时难觅i 2020-11-27 03:09

I want to enable standard copy paste for a TextView (the same as for EditText). How can I do it?

I tried using a non-editable EditText but it didn\'t work well (some

相关标签:
9条回答
  • 2020-11-27 03:13

    Requires API 11, Updated Code, previous method is deprecated

    Solution for theme full screen without ActionBar

    Extend TextView and in constructor paste following code

    this.setOnLongClickListener(new OnLongClickListener() {
    
                @Override
                public boolean onLongClick(View v) {
                    ClipboardManager cManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData cData = ClipData.newPlainText("text", getText());
                    cManager.setPrimaryClip(cData);
                    Util.toast(mContext, string.text_copyed);
                    return true;
                }
            });
    
    0 讨论(0)
  • 2020-11-27 03:13

    if someone wants to go the extra mile and do the select and copy to the clipboard with one click :

     phone.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("PhoneNumber", phone.getText());
                                clipboard.setPrimaryClip(clip);
    
                            }
                        });
    

    phone is the TextView and phone.Text is the Text that will be copied to the clipboard.

    0 讨论(0)
  • 2020-11-27 03:20

    In xml textview paste this code

    android:textIsSelectable="true"
    

    Then in java file,

     final TextView txtcopypaste = findViewById(R.id.txtcopypaste); // my textview
        txtcopypaste.setOnClickListener(new View.OnClickListener() { // set onclick listener to my textview
            @Override
            public void onClick(View view) {
                ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(txtcopypaste.getText().toString());              
                Toast.makeText(getApplicationContext(), "Copied :)", Toast.LENGTH_SHORT).show();
            }
        });
    

    Requirement : Need to copy and paste the text which is in the textview.

    OutCome : Using textview , once i clicked the textview. Its automatically copied the text which is in the textview.

    Note: While importing clipboardmanager try to prefer

    Please prefer text clipboard manager

    import android.text.ClipboardManager; // prefer this 
    

    try to avoid content clipboard manager

    import android.content.ClipboardManager; // Not this
    
    0 讨论(0)
  • 2020-11-27 03:23

    Try android:textIsSelectable.

    i.e., android:textIsSelectable="true"

    0 讨论(0)
  • 2020-11-27 03:23

    This works for copy pre-Honeycomb:

    import android.text.ClipboardManager;
    
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(textView.getText());
            Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
        }
    });
    
    0 讨论(0)
  • 2020-11-27 03:25

    To enable the standard copy/paste for TextView, U can choose one of the following:

    1. Change in layout file: add below property to your TextView

      android:textIsSelectable="true"

    2. In your Java class write this line to set it programmatically. myTextView.setTextIsSelectable(true);

    And long press on the TextView you can see copy/paste action bar.

    0 讨论(0)
提交回复
热议问题