public class PasteEditText extends EditText {
private Context context;
public PasteEditText(Context context) {
super(context);
this.context = context;
}
public PasteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
// TODO Auto-generated constructor stub
}
public PasteEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@SuppressLint("NewApi")
@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste) {
ClipboardManager clip = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
if (clip == null || clip.getText() == null) {
return false;
}
String text = clip.getText().toString();
if (text.startsWith(ChatActivity.COPY_IMAGE)) {
// intent.setDataAndType(Uri.fromFile(new
// File("/sdcard/mn1.jpg")), "image/*");
text = text.replace(ChatActivity.COPY_IMAGE, "");
Intent intent = new Intent(context, AlertDialog.class);
String str = "发送以下图片?";
intent.putExtra("title", str);
intent.putExtra("forwardImage", text);
intent.putExtra("cancel", true);
((Activity) context).startActivityForResult(intent, ChatActivity.REQUEST_CODE_COPY_AND_PASTE);
// clip.setText("");
}
}
return super.onTextContextMenuItem(id);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
if (!TextUtils.isEmpty(text) && text.toString().startsWith(ChatActivity.COPY_IMAGE)) {
setText("");
}
// else if(!TextUtils.isEmpty(text)){
// setText(SmileUtils.getSmiledText(getContext(),
// text),BufferType.SPANNABLE);
// }
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
}
61、PasteEditText - 自定义的EditText,用来处理复制粘贴的消息
最新推荐文章于 2022-08-24 10:04:08 发布
本文详细介绍了如何在Java中利用EditText实现剪切板粘贴功能,通过重写onTextContextMenuItem方法来监听并处理粘贴事件。包括使用ClipboardManager获取剪切板内容,并在文本开始时进行特殊处理,如移除特定的粘贴标记。同时,文章还展示了如何在粘贴操作后清空EditText内容,以及如何在粘贴文本前对其进行美化处理。
1547

被折叠的 条评论
为什么被折叠?



