您可以对从视图中选择的文本进行编码并复制到剪贴板。然后,如果您将该代码粘贴到任何其他应用程序中,您只能看到编码字符串。在您的应用程序中,您需要在粘贴到您的视图之前解码数据
在这里,我将提供代码来支持我的陈述:
public class MainActivity extends Activity {
EditText editText;
private ClipboardManager myClipboard;
private ClipData myClip;
String base64;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
editText = (EditText) findViewById(R.id.editText3);
editText.setCustomSelectionActionModeCallback(new Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.copy:
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText = editText.getText()
.subSequence(min, max);
String text = selectedText.toString();
byte[] data = null;
try {
data = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
base64 = Base64.encodeToString(data, Base64.DEFAULT);
myClip = ClipData.newPlainText("text", base64);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
// Finish and close the ActionMode
mode.finish();
return true;
case android.R.id.cut:
return true;
case android.R.id.paste:
int min2 = 0;
int max2 = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min2 = Math.max(0, Math.min(selStart, selEnd));
max2 = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText2 = editText.getText()
.subSequence(min2, max2);
String text2 = selectedText2.toString();
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item11 = abc.getItemAt(0);
String text1 = item11.getText().toString();
byte[] dataDecode = Base64.decode(text1, Base64.DEFAULT);
String textDecode = null;
try {
textDecode = new String(dataDecode, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editText.getText().replace(min2, max2, textDecode, 0,
textDecode.length());
Toast.makeText(getApplicationContext(), "Text Pasted",
Toast.LENGTH_SHORT).show();
// mode.finish();
return true;
default:
break;
}
return false;
}
});
}
}
这里我试图覆盖当用户长按 EditText 时出现在操作栏中的复制和粘贴方法。我的解决方案是在其他应用程序中隐藏数据的另一种方法。
当您单击文本插入句柄时出现粘贴弹出窗口,当您选择一些文本并长按它时出现粘贴选项,两者都与我的观察不同。
为了使用粘贴弹出窗口,我创建了一个自定义的 EditText 类,并在该类中定义了一个方法来完成所需的任务。这里是...
/**
* An EditText, which notifies when something was cut/copied/pasted inside it.
*/
public class MonitoringEditText extends EditText {
private final Context context;
private ClipboardManager myClipboard;
String base64;
EditText editText = (EditText) findViewById(R.id.editText3);
/*
* Just the constructors to create a new EditText...
*/
public MonitoringEditText(Context context) {
super(context);
this.context = context;
}
public MonitoringEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public MonitoringEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@Override
public boolean onTextContextMenuItem(int id) {
myClipboard = (ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
boolean consumed = super.onTextContextMenuItem(id);
switch (id) {
// case android.R.id.cut:
// onTextCut();
// break;
case android.R.id.paste: // here I am catching that pop up paste event
try {
onTextPaste();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
// case android.R.id.copy:
// onTextCopy();
//
}
return consumed;
}
// /**
// * Text was pasted into the EditText.
// * @throws UnsupportedEncodingException
// */
public void onTextPaste() throws UnsupportedEncodingException {
int min2 = 0;
int max2 = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min2 = Math.max(0, Math.min(selStart, selEnd));
max2 = Math.max(0, Math.max(selStart, selEnd));
}
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString(); //text is my encoded data
byte[] data = Base64.decode(text, Base64.DEFAULT);
String text1 = new String(data, "UTF-8"); //text1 is decoded data
editText.getText().replace(min2, max2, text1);
String fullText = editText.getText().toString();
editText.setText(fullText.replace(text, ""));
}
}