Copy text from TextView on Android

北战南征 提交于 2019-11-26 22:49:16

问题


I have a ListView where each item is a TextView.

I want to enable the long press behaviour similar to an EditText that displays the default context menu with items like "Select all", "Cut all", "Copy all", etc.

Is there an easy way to enable this for a TextView?


回答1:


I think I have a solution. Just call
registerForContextMenu(yourTextView);

and your TextView will be registered for receiving context menu events.

Then override onCreateContextMenu in your Activity:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "text that you want to show in the context menu - I use simply Copy");

    //cast the received View to TextView so that you can get its text
    TextView yourTextView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
    clipboard.setText(yourTextView.getText());
}

Hope this helps you and anyone else looking for a way to copy text from a TextView




回答2:


Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My code is here:

R.layout.edittext.xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:editable="false" 
android:background="@null"
android:textColor="#FFFFFF"/>

ListItemCopyTextActivity.java

public class ListItemCopyTextActivity extends Activity {    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ListView lv = new ListView(this);

    String[] values = new String[15];
    for (int i = 0; i < 15; i++) {
        values[i] = "ListItem NO." + i;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.edittext, values);
    lv.setAdapter(adapter);

    ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

    setContentView(ll);

    }
}

You can just long click the item, and choose the select text, copy, cut, past etc.




回答3:


To allow users to copy some or all of the TextView's value and paste it somewhere else,

set the XML attribute {@link android.R.styleable#TextView_textIsSelectable android:textIsSelectable} to "true"

or

call {@link #setTextIsSelectable setTextIsSelectable(true)}.




回答4:


You might want to register an onItemLongClickListener on your ListView and then based on the selected item, provide the user with whatever options you choose.




回答5:


I have a solution, but I'm not exactly to useful.

just use this method :

txtDescDetail.setCursorVisible(true);

i hope to do it.



来源:https://stackoverflow.com/questions/2952914/copy-text-from-textview-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!