How do I enable standard copy paste for a TextView

2019-01-04 09:49发布

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 (sometimes it became editable or the copy paste overlay was not shown). And it's probably not a good approach generally.

Need a working solution starting at API 7.

8条回答
在下西门庆
2楼-- · 2019-01-04 10:21

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;
            }
        });
查看更多
小情绪 Triste *
3楼-- · 2019-01-04 10:21
  1. use theme

    @android:style/Theme.Black.NoTitleBar.Fullscreen
    

    or

    @android:style/Theme.WithActionBar
    
  2. set TextView in xml

    android:textIsSelectable="true"
    
  3. see result

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-04 10: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();
    }
});
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-04 10:27

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
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 10:31

For an EditText, in manifest inside the activity use android:windowSoftInputMode="adjustResize"

查看更多
我命由我不由天
7楼-- · 2019-01-04 10:32

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.

查看更多
登录 后发表回答