Android-clipboard
Android-クリップボード
Androidは、さまざまな種類のデータをコピーして貼り付けるためのクリップボードフレームワークを提供します。 データは、テキスト、画像、バイナリストリームデータ、またはその他の複雑なデータタイプです。
AndroidはClipboardManagerとClipDataとClipData.itemのライブラリを提供し、コピーと貼り付けのフレームワークを使用します。クリップボードのフレームワークを使用するには、データをクリップオブジェクトに配置し、そのオブジェクトをシステム全体のクリップボードに配置する必要があります。
clipboardを使用するには、* getSystemService()*メソッドを呼び出してClipboardManagerのオブジェクトをインスタンス化する必要があります。 その構文は以下のとおりです-
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
データをコピーする
次に行う必要があるのは、ClipDataクラスのデータメソッドのそれぞれのタイプを呼び出して、ClipDataオブジェクトをインスタンス化することです。 テキストデータの場合、 newPlainText メソッドが呼び出されます。 その後、そのデータをClipboard Managerオブジェクトのクリップとして設定する必要があります。その構文は以下のとおりです-
ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
ClipDataオブジェクトはこれらの3つの形式を取ることができ、これらの形式を作成するために次の関数が使用されます。
Sr.No | ClipData Form & Method |
---|---|
1 |
Text newPlainText(ラベル、テキスト) 単一のClipData.Itemオブジェクトにテキスト文字列が含まれるClipDataオブジェクトを返します。 |
2 |
URI newUri(リゾルバ、ラベル、URI) 単一のClipData.ItemオブジェクトにURIが含まれるClipDataオブジェクトを返します。 |
3 |
Intent newIntent(ラベル、インテント) 単一のClipData.Itemオブジェクトにインテントが含まれるClipDataオブジェクトを返します。 |
貼り付けデータ
データを貼り付けるには、まず* getPrimaryClip()*メソッドを呼び出してクリップを取得します。 そして、そのクリックからClipData.Itemオブジェクトのアイテムを取得します。 そして、オブジェクトからデータを取得します。 その構文は以下のとおりです-
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();
これらのメソッドとは別に、クリップボードフレームワークを管理するためにClipboardManagerクラスによって提供される他のメソッドがあります。 これらの方法は以下のとおりです-
Sr.No | Method & description |
---|---|
1 |
getPrimaryClip() このメソッドは、クリップボード上の現在のプライマリクリップを返すだけです |
2 |
getPrimaryClipDescription() このメソッドは、クリップボード上の現在のプライマリクリップの説明を返しますが、そのデータのコピーは返しません。 |
3 |
hasPrimaryClip() 現在クリップボードにプライマリクリップがある場合、このメソッドはtrueを返します。 |
4 |
setPrimaryClip(ClipData clip) このメソッドは、クリップボードの現在のプライマリクリップを設定します |
5 |
setText(CharSequence text) このメソッドは、テキストをクリップボードにコピーするために直接使用できます |
6 |
getText() このメソッドは、クリップボードからコピーされたテキストを取得するために直接使用できます |
例
ClipboardManagerクラスの使用方法を示す例を次に示します。 テキストをコピーしてクリップボード経由で貼り付けることができる基本的なコピー貼り付けアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
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 の内容です。
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 の変更されたコンテンツです。
<?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 の内容です。
<resources>
<string name="app_name">My Application</string>
</resources>
以下は 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>
修正したばかりのアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Androidスタジオのインストーラーは、次の画像を表示します-
[コピーするテキスト]フィールドにテキストを入力し、[テキストのコピー]ボタンを選択します。 以下に示す次の通知が表示されます-
貼り付けボタンを押すだけで、コピーされたテキストがコピーされたテキストのフィールドに貼り付けられます。 以下に示されています-