ClipboardManager是剪貼板服務的接口, 在剪貼板上放置和檢索文字.
你不直接實例化這個類,而是通過getSystemService(String)取得ClipboardManager. 再利用setText(CharSequence text)和getText()方法放置和檢索文字.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/copyto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Copy To ClipBoard"
/>
<Button
android:id="@+id/pastefrom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Paste From ClipBoard"
/>
</LinearLayout>
AndroidClipBoard.java
package com.AndroidClipBoard;
import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidClipBoard extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText editText = (EditText)findViewById(R.id.edittext);
Button buttonCopyTo = (Button)findViewById(R.id.copyto);
Button buttonPasteFrom = (Button)findViewById(R.id.pastefrom);
final ClipboardManager myClipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
buttonCopyTo.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myClipBoard.setText(editText.getText());
}});
buttonPasteFrom.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(myClipBoard.getText());
}});
}
}
沒有留言:
發佈留言