2

My app with two activities is targeted to run on Android 4 or above. Activity 1 displays a Webview, and Activity 2 consists of a Listview and an EditText.

I would like to programmatically send/export any text copied in the Webview of Activity 1 to the EditText of Activity 2 once a button is pressed.

To make it clear, my purpose is to create a button to listen to any text copied to clipboard. When the button is pressed, Activity 2 will be called and the text in clipboard will be sent/pasted to the EditText (edtbox) of Activity 2

So far, I have applied the following code lines:

Activity 1 (on button clicked):

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
clipboard.setText("Text to copy");
clipboard.getText();

Intent i = new Intent(Activity1.this, Activity2.class);
Bundle bundle=new Bundle();
bundle.putString("clipboard", "Android");
i.putExtras(bundle);   
startActivity(i);

Activity 2 (under onCreate):

Bundle bundle=getIntent().getExtras();
if(bundle !=null)
{
    String name=bundle.getString("clipboard");
    EditText edttxt=(EditText)findViewById(R.id.edtbox);
    edttxt.setText(name);
}

However, Activity 2 fails to be loaded and Eclipse throws a crash. I guess I haven't sent clipboard text to EditText of Activity 2 but don't know how to do.

I wonder whether you guys can help me to solve this problem. Thank you very much in advance.

=====

UPDATED:

Here is the Eclipse LogCat:

4

2 回答 2

1

试试这个::

活动 1(点击按钮):

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
clipboard.setText("Text to copy");
Sting cpytext = clipboard.getText();

Intent i = new Intent(Activity1.this, Activity2.class);
//Bundle bundle=new Bundle();
//bundle.putString("clipboard", cpytext);
i.putExtra("clipboard", cpytext);   
startActivity(i);

活动 2(在 onCreate 下):

if(getIntent().hasExtra("clipboard")) {
     String name = (getIntent().getStringExtra("clipboard"));
     EditText edttxt=(EditText)findViewById(R.id.edtbox);
     edttxt.setText(name);
}
于 2012-08-07T12:13:44.270 回答
1

我不明白你为什么不能这样使用......

 ====>At Activity 1

 Intent i = new Intent(Activity1.this, Activity2.class);
 //Bundle bundle=new Bundle();
 //bundle.putString("clipboard", "Android");
  i.putExtras("clipboard", "Android");
  startActivity(i);


 ====>At Activity 2
EditText edttxt=(EditText)findViewById(R.id.edtbox);//place edit text declaration out side of the Bundle Extras block
Bundle bundle=getIntent().getExtras();
if(bundle !=null)
{
  String name=bundle.getString("clipboard");
  edttxt.setText(name);
}
于 2012-08-07T11:31:16.493 回答