从 Android 上的 TextView 复制文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2952914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:02:24  来源:igfitidea点击:

Copy text from TextView on Android

androidselectcopycontextmenutextview

提问by Erdal

I have a ListViewwhere each item is a TextView.

我有一个ListView,其中每个项目都是一个TextView.

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

我想启用类似于EditText显示默认上下文菜单的长按行为,其中包含“全选”、“全部剪切”、“全部复制”等项目。

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

有没有一种简单的方法可以为 a 启用此功能TextView

回答by pandre

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

我想我有一个解决方案。打电话就行
registerForContextMenu(yourTextView);

and your TextViewwill be registered for receiving context menu events.

并且您TextView将注册接收上下文菜单事件。

Then override onCreateContextMenuin your Activity:

然后覆盖onCreateContextMenu你的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

希望这可以帮助您和其他任何正在寻找从文本复制文本的方法的人 TextView

回答by Ruobin Wang

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:

实际上,您不必自己开发此功能。您只需要使用 EditText 而不是 TextView,同时将 EditText 的 android:editable 设置为 false。我的代码在这里:

R.layout.edittext.xml

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

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.

您只需长按该项目,然后选择选择文本、复制、剪切、过去等。

回答by Samuel

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

为了允许用户复制部分或全部 TextView 的值并将其粘贴到其他地方,

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

将 XML 属性设置{@link android.R.styleable#TextView_textIsSelectable android:textIsSelectable}"true"

or

或者

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

打电话{@link #setTextIsSelectable setTextIsSelectable(true)}

回答by Savvas Dalkitsis

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.

您可能希望在 ListView 上注册一个 onItemLongClickListener,然后根据所选项目为用户提供您选择的任何选项。

回答by mohammadreza

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

我有一个解决方案,但我并不完全有用。

just use this method :

只需使用此方法:

txtDescDetail.setCursorVisible(true);

i hope to do it.

我希望能做到。