textview 复制粘贴

In this tutorial, we’ll implement the copy-paste feature on a TextView in our Android Application.
At the end of this article, you’d be able to copy a text to the Android System’s Clipboard and then paste that Clipboard data.

在本教程中,我们将在Android应用程序的TextView上实现复制粘贴功能。
在本文的结尾,您可以将文本复制到Android系统的剪贴板中,然后粘贴该剪贴板数据。

Andorid剪贴板–复制粘贴 (Andorid Clipboard – Copy Paste)

You must have noticed that EditText has a built in Clipboard Manager for Copy Paste functionality.
But a TextView doesn’t. So in order to allow Copy Pasting TextViews, we need to register the ContextMenu.

您必须已经注意到EditText具有内置的Clipboard Manager用于复制粘贴功能。
但是TextView却没有。 因此,为了允许复制粘贴TextView,我们需要注册ContextMenu。

Hence in order to receive the menu events, we need to registerForContextMenu.
Once this is done, you can long-press on the TextView to display the menu.

因此,为了接收菜单事件,我们需要registerForContextMenu
完成此操作后,您可以长按TextView以显示菜单。

But where’s the menu?

但是菜单在哪里?

The menu is created in the onCreateContextMenu() method.
The menu items actions are set in the onContextItemSelected method.

菜单是在onCreateContextMenu()方法中创建的。
菜单项操作在onContextItemSelected方法中设置。

将文本复制到剪贴板 (Copying Text To Clipboard)

It’s easy to copy TextView text onto the Clipboard. You just need to set the ClipData type as newPlainText and pass the string.

将TextView文本复制到剪贴板很容易。 您只需要将ClipData类型设置为newPlainText并传递字符串即可。

Example:

例:

ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text", textView.getText());
manager.setPrimaryClip(clipData);

将文本粘贴到剪贴板 (Pasting Text To Clipboard)

Now that you have copied the text onto the Clipboard, just paste it using the following piece of code:

现在您已将文本复制到剪贴板上,只需使用以下代码将其粘贴:

ClipData pasteData = manager.getPrimaryClip();
ClipData.Item item = pasteData.getItemAt(0);
String paste = item.getText().toString();

Here we’re pasting the first data from the Clipboard. If we have multiple copied texts, they can be pasted using pasteData.getItemAt(1) and so on.

在这里,我们粘贴剪贴板中的第一个数据。 如果我们有多个复制的文本,则可以使用pasteData.getItemAt(1)进行粘贴,依此类推。

Now let’s jump onto the implementation part of this article.
In the next section, we’ll be developing a simple Android Application in which you can copy-paste text from one TextView to another.

现在,让我们跳到本文的实现部分。
在下一节中,我们将开发一个简单的Android应用程序,您可以在其中将文本从一个TextView复制粘贴到另一个。

项目结构 (Project Structure)

Android Textview Copy Paste Project Structure

Android Textview复制粘贴项目结构

码 (Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/tvCopy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Welcome To Journaldev.com"android:padding="8dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tvPaste"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Paste here"android:padding="8dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/tvCopy" /></androidx.constraintlayout.widget.ConstraintLayout>

The code for the MainActivity.java class is given below:

MainActivity.java类的代码如下:

package com.journaldev.androidtextviewcopypaste;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {TextView tvCopy, tvPaste;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvCopy = findViewById(R.id.tvCopy);tvPaste = findViewById(R.id.tvPaste);registerForContextMenu(tvCopy);registerForContextMenu(tvPaste);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);menu.setHeaderTitle("Options");switch (v.getId()) {case R.id.tvCopy:menu.add(0, v.getId(), 0, "Copy");TextView textView = (TextView) v;ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);ClipData clipData = ClipData.newPlainText("text", textView.getText());if (manager != null) {manager.setPrimaryClip(clipData);}break;case R.id.tvPaste:menu.add(0, v.getId(), 0, "Paste");break;}}@Overridepublic boolean onContextItemSelected(@NonNull MenuItem item) {switch (item.getItemId()) {case R.id.tvCopy:Toast.makeText(getApplicationContext(), "Copy Clicked", Toast.LENGTH_LONG).show();break;case R.id.tvPaste:ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);if (manager != null && manager.getPrimaryClip() != null && manager.getPrimaryClip().getItemCount() > 0) {tvPaste.setText(manager.getPrimaryClip().getItemAt(0).getText().toString());}break;}return super.onContextItemSelected(item);}
}

getItemCount() is used to get the items present in the clipboard.
You can also set the clipboard data to be copied in the onContextItemSelected copy menu item action.

getItemCount()用于获取剪贴板中存在的项目。
您还可以在onContextItemSelected复制菜单项操作中设置要复制的剪贴板数据。

输出量 (Output)

The output of the above application in action is given below:

上面应用程序的输出如下:

Android Textview Copy Paste Output

Android Textview复制粘贴输出

That sums up this tutorial. Can you add a Cut Menu option? Post your code snippets in the comment sections!

总结了本教程。 您可以添加剪切菜单选项吗? 在注释部分中发布您的代码段!

下载链接 (Download Links)

You can do ahead and download the full source code from the link below or visit our Github Repository.

您可以继续并从下面的链接下载完整的源代码,或者访问我们的Github存储库。

AndroidTextViewCopyPasteAndroidTextViewCopyPaste
Github RepositoryGithub存储库

翻译自: https://www.journaldev.com/33255/android-textview-copy-paste-using-clipboard

textview 复制粘贴

textview 复制粘贴_Android TextView使用剪贴板复制粘贴相关推荐

  1. 电脑连续复制粘贴,随意复制,随意粘贴,需要打开剪贴板功能。

    电脑连续复制粘贴,随意粘贴,需要打开剪贴板功能. 以win10为例,连续复制粘贴步骤如下所示: 1.在电脑桌面点击windows图标. 2.点击齿轮状图标,进入设置. 3.在windows设置中,点击 ...

  2. ABAP实现粘贴板的操作,复制粘贴

    ABAP可以实现复制粘贴的操作 自己玩儿 项目里暂时用不到 在ABAP中的 CLASS :CL_GUI_FRONTEND_SERVICES中提供了两个方法来控制与剪贴板内容的导入导出, 分别是:CLI ...

  3. idea复制java_IntelliJ IDEA的剪切、复制和粘贴

    IntelliJ IDEA的剪切.复制和粘贴 本节内容概览: • 剪切.复制和粘贴的基本使用 • 复制选定的文本片段 • 将路径复制到文件 • 将引用复制到一行或一个符号 • 剪切选定的文本片段 • ...

  4. word vba 转换html,通过VBA和剪贴板复制HTML到Word丢失特殊字符

    我想通过VBA将一些HTML格式的数据粘贴到Word. HTML数据是从MS XML获得的,通过将xml文档通过给定的xsl转换为合适的html,并将这个转换后的html数据转换为保留HTML格式的W ...

  5. html页面怎样禁止复制粘贴,javascript中如何禁止复制粘贴?

    在javascript中可以使用oncopy事件和onpaste事件来实现禁止复制粘贴的功能.oncopy事件会在用户拷贝元素上内容时被触发:onpaste事件在用户向元素中粘贴文本时触发. java ...

  6. 浏览器复制粘贴以及手机端webview复制粘贴

    一.浏览器赋值粘贴 1.document.execCommand 这部分参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API/Document/ ...

  7. 禁止复制粘贴_网页禁止你复制粘贴?新同事教我这三招,全网内容随我复制

    我们平常经常需要在网上查找资料,但是由于网站的限制,网页的内容经常没办法复制粘贴,只能自己一个字一个字手打,但这样效率实在太慢了,今天新同事教了我三个方法,轻松就可以直接复制网页内容,想复制哪里就复制 ...

  8. arcgis新建图层信息复制_ArcGIS中的数据库之间复制和粘贴数据

    请注意,ArcGIS 无法粘贴其不支持的数据类型或定义.这意味着在目标数据库中创建的表或要素类将包含: *ArcGIS 支持的数据类型 任何无法映射到 ArcGIS 数据类型的列都不会在目标数据库的要 ...

  9. 计算机复制操作的方法,怎么用键盘复制粘贴?电脑使用键盘复制粘贴的方法

    电脑对于我们来说是很常见的,无论是我们生活还是学习,以及工作等等都会用到电脑,所以对于一些比较常见的操作我们需要知道.就拿键盘复制粘贴来说,这个是最基本的,因此熟练地掌握鼠标键盘进行复制粘贴是很有必要 ...

最新文章

  1. python with上下文管理
  2. 面试官问:Kafka 会不会丢消息?怎么处理的?
  3. COM:根系-土壤-微生物互作
  4. Linux下安装和使用boost库
  5. 在Android中,如何以编程方式在dp中设置边距?
  6. python 多进程 multiprocessing 进程池 pool apply_async()函数与apply()函数的用法
  7. Hibernate之必须导入jar包
  8. C++(16)--运算符重载(自定义Integer类)
  9. C++ std::move()和完美转发
  10. Android学习之自定义标题栏
  11. ROS学习笔记(一)——软件版本的选择
  12. create-react-app 支持多入口
  13. 极路由+锐捷校园网小白使用教程
  14. 一转眼,波士顿动力 Atlas 机器人又会过独木桥了!
  15. 高数 | 函数在间断点处的极值问题
  16. 计算机管理信息阶段性测验答案,管理信息系统阶段性学习测验一试题及答案(14页)-原创力文档...
  17. 运用frame、frameset框架不显示问题
  18. silverlight,WPF动画终极攻略之会飞的小鸟篇(Blend 4开发)
  19. 天下难事,必作于易;天下大事,必作于细。
  20. STM32固件库常见命名方式

热门文章

  1. UIPickerView用法(左右比例,整体大小,字体大小)
  2. inner join ,left join ,right join 以及java时间转换
  3. 各数据类型的取值范围及占用字节数。。
  4. Interesting Finds: 2007.12.19
  5. 如何把 Google adsense 的广告放到博客的Banner位置
  6. [转载] set集合python_python基础-set集合
  7. PHP操作tcpdf插件生成PDF
  8. 高通HAL层之Sensor HAL
  9. 请求重定向与请求转发的区别
  10. 【ms access】SQL 引用外部表