Android 简明教程

Android - Clipboard

Android 提供了用于复制和粘贴不同类型数据的剪贴板框架。数据可以是文本、图像、二进制流数据或其他复杂的数据类型。

Android provides the clipboard framework for copying and pasting different types of data. The data could be text, images, binary stream data or other complex data types.

Android 提供了 ClipboardManager 库和 ClipData 和 ClipData.item 以便使用复制和粘贴框架。为了使用剪贴板框架,您需要将数据放入剪贴对象中,然后将该对象放入系统范围的剪贴板中。

Android provides the library of ClipboardManager and ClipData and ClipData.item to use the copying and pasting framework.In order to use clipboard framework, you need to put data into clip object, and then put that object into system wide clipboard.

为了使用剪贴板,您需要通过调用 getSystemService() 方法实例化 ClipboardManager 对象。它的语法如下所示 −

In order to use clipboard , you need to instantiate an object of ClipboardManager by calling the getSystemService() method. Its syntax is given below −

ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

Copying data

接下来,您需要通过调用 ClipData 类的相应类型的数据方法来实例化 ClipData 对象。对于文本数据,将调用 newPlainText 方法。之后,您必须将该数据设置为剪贴板管理器对象的剪贴。它的语法如下所示 −

The next thing you need to do is to instantiate the ClipData object by calling the respective type of data method of the ClipData class. In case of text data , the newPlainText method will be called. After that you have to set that data as the clip of the Clipboard Manager object.Its syntax is given below −

ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

ClipData 对象可以采用这三种形式,并且以下函数用于创建这些形式。

The ClipData object can take these three form and following functions are used to create those forms.

Sr.No

ClipData Form & Method

1

Text newPlainText(label, text) Returns a ClipData object whose single ClipData.Item object contains a text string.

2

URI newUri(resolver, label, URI) Returns a ClipData object whose single ClipData.Item object contains a URI.

3

Intent newIntent(label, intent) Returns a ClipData object whose single ClipData.Item object contains an Intent.

Pasting data

为了粘贴数据,我们将首先通过调用 getPrimaryClip() 方法获取剪贴。然后从该点击,我们将获得 ClipData.Item 对象中的项目。然后从对象中获取数据。它的语法如下所示 −

In order to paste the data, we will first get the clip by calling the getPrimaryClip() method. And from that click we will get the item in ClipData.Item object. And from the object we will get the data. Its syntax is given below −

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

除了这些方法之外,ClipboardManager 类还提供了其他方法来管理剪贴板框架。这些方法在下表中列出:

Apart from the these methods , there are other methods provided by the ClipboardManager class for managing clipboard framework. These methods are listed below −

Sr.No

Method & description

1

getPrimaryClip() This method just returns the current primary clip on the clipboard

2

getPrimaryClipDescription() This method returns a description of the current primary clip on the clipboard but not a copy of its data.

3

hasPrimaryClip() This method returns true if there is currently a primary clip on the clipboard

4

setPrimaryClip(ClipData clip) This method sets the current primary clip on the clipboard

5

setText(CharSequence text) This method can be directly used to copy text into the clipboard

6

getText() This method can be directly used to get the copied text from the clipboard

Example

下面是一个演示如何使用 ClipboardManager 类的示例。它创建一个基本的复制粘贴应用程序,允许您复制文本,然后通过剪贴板粘贴它。

Here is an example demonstrating the use of ClipboardManager class. It creates a basic copy paste application that allows you to copy the text and then paste it via clipboard.

要使用此示例,你可以在实际设备或模拟器上运行此示例。

To experiment with this example , you can run this on an actual device or in an emulator.

Steps

Description

1

You will use Android studio IDE to create an Android application and under a package com.example.sairamkrishna.myapplication.

2

Modify src/MainActivity.java file to add necessary code.

3

Modify the res/layout/activity_main to add respective XML components

4

Run the application and choose a running android device and install the application on it and verify the results

以下是修改的主活动文件 src/MainActivity.java 的内容。

Following is the content of the modified main activity file src/MainActivity.java.

package com.example.sairamkrishna.myapplication;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
   EditText ed1, ed2;
   Button b1, b2;

   private ClipboardManager myClipboard;
   private ClipData myClip;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1 = (EditText) findViewById(R.id.editText);
      ed2 = (EditText) findViewById(R.id.editText2);

      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);

      myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

      b1.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
            String text;
            text = ed1.getText().toString();

            myClip = ClipData.newPlainText("text", text);
            myClipboard.setPrimaryClip(myClip);

            Toast.makeText(getApplicationContext(), "Text Copied",
               Toast.LENGTH_SHORT).show();
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
            ClipData abc = myClipboard.getPrimaryClip();
            ClipData.Item item = abc.getItemAt(0);

            String text = item.getText().toString();
            ed2.setText(text);

            Toast.makeText(getApplicationContext(), "Text Pasted",
               Toast.LENGTH_SHORT).show();
         }
      });
   }

}

以下是修改后的 xml res/layout/activity_main.xml 内容。

Following is the modified content of the xml res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android:text="Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />

   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Copy text"
      android:layout_below="@+id/imageView"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:hint="paste text"
      android:layout_below="@+id/editText"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Copy text"
      android:id="@+id/button"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Paste text"
      android:id="@+id/button2"
      android:layout_below="@+id/editText2"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />

</RelativeLayout>

以下是 res/values/string.xml 的内容。

Following is the content of the res/values/string.xml.

<resources>
   <string name="app_name">My Application</string>
</resources>

下面是 AndroidManifest.xml 文件的内容。

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.sairamkrishna.myapplication.MainActivity"
         android:label="@string/app_name" >

         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>

      </activity>
   </application>

</manifest>

让我们尝试运行我们刚刚修改过的应用程序。我假设您在进行环境设置时已创建了 AVD 。要在 Android Studio 中运行此应用,请打开项目的某个活动文件,然后从工具栏中单击 Run 图标。Android Studio 安装程序将显示以下图像:

Let’s try to run our an application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project’s activity files and click Run icon from the tool bar. Android studio installer will display following images −

clipboard

现在只需在 Text to copy 域中输入任意文本,然后选择 copy text 按钮。将显示以下通知,如下所示:

Now just enter any text in the Text to copy field and then select the copy text button. The following notification will be displayed which is shown below −

clipboard1

现在只需按下粘贴按钮,您将看到所复制的文本现已粘贴在 Copied Text 域中。如下所示:

Now just press the paste button, and you will see the text which is copied is now pasted in the field of Copied Text. It is shown below −

clipboard2