Android Studio Tutorial – Clipboard (работа с буфером обмена)

[:ru]Андроид обеспечивает clipboard framework (фреймворк буфера обмена) для копирования и вставки различных типов данных. Данные могут быть разных типов: текст, изображения, двоичный поток данных или другие комплексные типы данных.

Android предоставляет библиотеки ClipboardManager, ClipData и ClipData.item фреймворка для копирования и вставки. Для того, чтобы использовать clipboard framework, вы должны поместить данные в объект clip, а затем методом put отправить объект в системный буфер обмена.

Исходный код примера:

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="dev.edmt.clipboard.MainActivity">

    <LinearLayout
        android:id="@+id/myLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edtCopy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Copy Text"
            />

        <EditText
            android:id="@+id/edtPaste"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Paste Text"
            />
    </LinearLayout>

    <LinearLayout
        android:weightSum="2"
        android:layout_below="@+id/myLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnCopy"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Copy Text"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_red_dark"
            />

        <Button
            android:id="@+id/btnPaste"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Paste Text"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_green_dark"
            />
    </LinearLayout>
</RelativeLayout>
import android.content.ClipData;
import android.content.ClipboardManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edtCopy,edtPaste;
    Button btnCopy,btnPaste;

    ClipboardManager clipboardManager;
    ClipData clipData;


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

        edtCopy = (EditText)findViewById(R.id.edtCopy);
        edtPaste=(EditText)findViewById(R.id.edtPaste);

        btnCopy = (Button)findViewById(R.id.btnCopy);
        btnPaste=(Button)findViewById(R.id.btnPaste);

        clipboardManager=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        btnCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = edtCopy.getText().toString();
                clipData = ClipData.newPlainText("text",text);
                clipboardManager.setPrimaryClip(clipData);

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

        btnPaste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipData data = clipboardManager.getPrimaryClip();
                ClipData.Item item = data.getItemAt(0);

                String text = item.getText().toString();
                edtPaste.setText(text);
                Toast.makeText(getApplicationContext(),"Text Pasted ",Toast.LENGTH_SHORT).show();
            }
        });


    }
}

 [:en]Android provides the clipboard framework (framework clipboard) to copy and paste different types of data. Data can be of different types: text, images, binary stream data or other complex data types.

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

Source code example:

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="dev.edmt.clipboard.MainActivity">

    <LinearLayout
        android:id="@+id/myLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edtCopy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Copy Text"
            />

        <EditText
            android:id="@+id/edtPaste"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Paste Text"
            />
    </LinearLayout>

    <LinearLayout
        android:weightSum="2"
        android:layout_below="@+id/myLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnCopy"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Copy Text"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_red_dark"
            />

        <Button
            android:id="@+id/btnPaste"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Paste Text"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_green_dark"
            />
    </LinearLayout>
</RelativeLayout>
import android.content.ClipData;
import android.content.ClipboardManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edtCopy,edtPaste;
    Button btnCopy,btnPaste;

    ClipboardManager clipboardManager;
    ClipData clipData;


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

        edtCopy = (EditText)findViewById(R.id.edtCopy);
        edtPaste=(EditText)findViewById(R.id.edtPaste);

        btnCopy = (Button)findViewById(R.id.btnCopy);
        btnPaste=(Button)findViewById(R.id.btnPaste);

        clipboardManager=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        btnCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = edtCopy.getText().toString();
                clipData = ClipData.newPlainText("text",text);
                clipboardManager.setPrimaryClip(clipData);

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

        btnPaste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipData data = clipboardManager.getPrimaryClip();
                ClipData.Item item = data.getItemAt(0);

                String text = item.getText().toString();
                edtPaste.setText(text);
                Toast.makeText(getApplicationContext(),"Text Pasted ",Toast.LENGTH_SHORT).show();
            }
        });


    }
}

 [:]

Додати коментар