Lập trình Android cung cấp thư viện của ClipboardManager và ClipData và ClipData.item để sử dụng khung sao chép và dán. Để sử dụng khung công cụ clipboard, bạn cần đưa dữ liệu vào đối tượng clip và sau đó đặt đối tượng đó vào khay nhớ tạm của hệ thống.
Để sử dụng clipboard, bạn cần khởi tạo một đối tượng của ClipboardManager bằng cách gọi phương thức getSystemService () . Cú pháp của nó được đưa ra dưới đây
ClipboardManager myClipboard; myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Sao chép dữ liệu
Điều tiếp theo bạn cần làm là khởi tạo đối tượng ClipData bằng cách gọi kiểu tương ứng của phương thức dữ liệu của lớp ClipData.Trong trường hợp dữ liệu văn bản, phương thức newPlainText sẽ được gọi. Sau đó bạn phải thiết lập dữ liệu đó dưới dạng clip của đối tượng Clipboard Manager object.Its được đưa ra dưới đây -
ClipData myClip; String text = "hello world"; myClip = ClipData.newPlainText("text", text); myClipboard.setPrimaryClip(myClip);Đối tượng ClipData có thể thực hiện ba dạng và các hàm sau được sử dụng để tạo các biểu mẫu này.
| Sr.No | Biểu mẫu & phương thức ClipData |
|---|---|
| 1 | Bản văn newPlainText (nhãn, văn bản) Trả về một đối tượng ClipData có đối tượng ClipData.Item duy nhất chứa một chuỗi văn bản. |
| 2 | URI newUri (trình phân giải, nhãn, URI) Trả về đối tượng ClipData có đối tượng ClipData.Item đơn chứa URI. |
| 3 | Ý định newIntent (nhãn, ý định) Trả về một đối tượng ClipData có đối tượng ClipData.Item đơn chứa Intent. |
Đang dán dữ liệu
Để dán dữ liệu, đầu tiên chúng ta sẽ nhận được clip bằng cách gọi phương thức getPrimaryClip () . Và từ nhấp chuột đó, chúng ta sẽ nhận được mục trong đối tượng ClipData.Item.Và từ đối tượng, chúng ta sẽ lấy dữ liệu. Cú pháp của nó được đưa ra dưới đây -
ClipData abc = myClipboard.getPrimaryClip(); ClipData.Item item = abc.getItemAt(0); String text = item.getText().toString();Ngoài các phương thức này, còn có các phương thức khác do lớp ClipboardManager cung cấp để quản lý khung công việc tạm thời. Những phương pháp này được liệt kê dưới đây
| Sr.No | Phương pháp & mô tả |
|---|---|
| 1 | getPrimaryClip () Phương thức này chỉ trả về clip chính hiện tại trên clipboard |
| 2 | getPrimaryClipDescription () Phương thức này trả về một mô tả của clip chính hiện tại trên clipboard nhưng không phải là bản sao dữ liệu của nó. |
| 3 | hasPrimaryClip () Phương thức này trả về true nếu hiện có một clip chính trên clipboard |
| 4 | setPrimaryClip (Clip Clip) Phương thức này đặt clip chính hiện tại vào clipboard |
| 5 | setText (văn bản CharSequence) Phương pháp này có thể được sử dụng trực tiếp để sao chép văn bản vào clipboard |
| 6 | getText () Phương pháp này có thể được sử dụng trực tiếp để lấy văn bản đã sao chép từ khay nhớ tạm |
Thí dụ
Đây là một ví dụ minh họa việc sử dụng lớp ClipboardManager. Nó tạo ra một ứng dụng dán bản sao cơ bản cho phép bạn sao chép văn bản và sau đó dán nó vào clipboard.Để thử nghiệm với ví dụ này, bạn có thể chạy nó trên một thiết bị thực tế hoặc trong một trình giả lập.
| Các bước | Sự miêu tả |
|---|---|
| 1 | Bạn sẽ sử dụng Android studio IDE để tạo ứng dụng Android và theo gói com.example.sairamkrishna.myapplication. |
| 2 | Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết. |
| 3 | Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng |
| 4 | Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả |
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(); } }); } }Sau đây là nội dung sửa đổi của 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>Sau đây là nội dung của res / values / string.xml .
<resources> <string name="app_name">My Application</string> </resources>Sau đây là nội dung của tệp AndroidManifest.xml .
<?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>Hãy thử chạy một ứng dụng mà chúng ta vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.
Để chạy ứng dụng từ lập trình Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào Chạy biểu tượng từ thanh công cụ. Trình cài đặt Android studio sẽ hiển thị hình ảnh sau đây -
![]() |
| Học lập trình Android |
Bây giờ chỉ cần nhập bất kỳ văn bản trong trường Văn bản để sao chép và sau đó chọn nút sao chép văn bản. Thông báo sau sẽ được hiển thị được hiển thị bên dưới
![]() |
| Học lập trình Android |
Bây giờ chỉ cần nhấn nút dán, và bạn sẽ thấy văn bản được sao chép hiện được dán trong trường Văn bản đã sao chép. Nó được hiển thị dưới đây
![]() |
| Học lập trình Android |



Không có nhận xét nào:
Đăng nhận xét