import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.EditText;
public class ClipboardTest extends Activity {
private EditText edit ;
private EditText edit2 ;
private ClipboardManager clipboard;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
edit = (EditText)findViewById(R.id.edit);
edit2 = (EditText)findViewById(R.id.edit2);
edit.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick(View v){
popClipfunc();
return true;
}
});
edit2.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick(View v){
popClipfunc();
return true;
}
});
}
private void popClipfunc(){
new AlertDialog.Builder(ClipboardTest.this)
.setItems(R.array.clipboard_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0:
SelectAll();
break;
case 1:
CutAll();
break;
case 2:
CopyAll();
break;
case 3:
Paste();
break;
}
/* User clicked so do some stuff
String[] items = getResources().getStringArray(R.array.clipboard_items);
new AlertDialog.Builder(ClipboardTest.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();
*/
}
})
.show();
}
private void SelectAll(){
}
private void CutAll(){
EditText v = (EditText)getCurrentFocus();
copyToClipboard(v.getText().toString());
v.setText("");
}
private void CopyAll(){
EditText v = (EditText)getCurrentFocus();
String s = v.getText().toString();
if(s != null)
copyToClipboard(s);
}
private void Paste(){
EditText v = (EditText)getCurrentFocus();
String s = v.getText().toString();
if(clipboard.hasText()){
v.setText(s+copyFromClipboard());
}
}
private void copyToClipboard(String s){
clipboard.setText(s);
}
private CharSequence copyFromClipboard(){
return clipboard.getText();
}
}
========================================================
array.xml ( values dir )
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="clipboard_items">
<item>Select all</item>
<item>Cut all</item>
<item>Copy all</item>
<item>Paste</item>
</string-array>
</resources>