edittext实现粘贴表情

 1 package com.sixin.view;

 2 

 3 import com.sixin.utile.FaceDataUtil;

 4 import android.annotation.SuppressLint;

 5 import android.content.Context;

 6 import android.text.Editable;

 7 import android.util.AttributeSet;

 8 import android.widget.EditText;

 9 

10 public class CustomEditView extends EditText {

11     private static final int ID_PASTE = android.R.id.paste;

12 

13     public CustomEditView(Context context, AttributeSet attrs) {

14         super(context, attrs);

15     }

16 

17     @SuppressLint("NewApi")

18     @Override

19     public boolean onTextContextMenuItem(int id) {

20         if (id == ID_PASTE) {

21             try {

22                 int currentapiVersion = android.os.Build.VERSION.SDK_INT;

23                 if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {

24                     android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

25                     String value = clipboard.getText().toString();

26                     Editable edit = getEditableText();

27                     // edit.clear();

28                     int index = this.getSelectionStart();

29                     if (index < 0 || index >= edit.length()) {

30                         edit.append(FaceDataUtil.getIns(getContext()).getExpressionString(getContext(), value, 0.7f));

31                     } else {

32                         edit.insert(index, FaceDataUtil.getIns(getContext()).getExpressionString(getContext(), value, 0.7f));// 光标所在位置插入文字

33                     }

34 

35                 } else {

36                     android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

37                     String value = clipboard.getText().toString();

38                     Editable edit = getEditableText();

39                     // edit.clear();

40                     int index = this.getSelectionStart();

41                     if (index < 0 || index >= edit.length()) {

42                         edit.append(FaceDataUtil.getIns(getContext()).getExpressionString(getContext(), value, 0.7f));

43                     } else {

44                         edit.insert(index, FaceDataUtil.getIns(getContext()).getExpressionString(getContext(), value, 0.7f));// 光标所在位置插入文字

45                     }

46                 }

47                 return true;

48             } catch (Exception e) {

49                 e.printStackTrace();

50             }

51         }

52         return super.onTextContextMenuItem(id);

53     }

54 

55 }

 

你可能感兴趣的:(EditText)