2011-03-02 32 views
7

Có cách nào để nắm bắt sự kiện dán trong ứng dụng của tôi không? Tôi phải làm một cái gì đó khi tôi nhấp vào dài trên một editText và chọn Paste từ menu ngữ cảnh. Cảm ơnsự kiện dán android

Trả lời

4

Tạo menu.xml với vị trí 'dán'

Đăng ký ContextMenu để EditText bạn

EditText et=(EditText)findViewById(R.id.et); 
registerForContextMenu(et); 

Tạo ContextMenu

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu);  
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; 
    menu.setHeaderTitle("title"); 
} 

Tạo trình đơn phương thức onClick

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();  
    switch (item.getItemId()) { 
    case R.id.paste:  
     break;  
    } 
    return true; 
} 
+6

Tôi biết chủ đề này là cũ, nhưng không thay thế này được xây dựng trong menu ngữ cảnh với menu ngữ cảnh của riêng bạn? Nếu bạn thích menu đó, nhưng chỉ muốn được thông báo khi dán xảy ra? –

+0

@ScottBiggs Nó cũng hoạt động mà không cần tạo lại menu. Xem câu trả lời của tôi [ở đây] (http://stackoverflow.com/a/14981376/717341) –

5

Bạn nên triển khai Trình nghe văn bản TextWatcher trên điều khiển nhận hành động dán.

Lớp Trình soạn thảo văn bản cung cấp các phương pháp để xử lý OnChange, BeforeChange và AfterChange của bất kỳ chỉnh sửa nào. Ví dụ:

private void pasteEventHandler() { 
    ((EditText)findViewById(R.id.txtOutput)) 
      .addTextChangedListener(new TextWatcher() { 

       public void afterTextChanged(Editable s) { 
        Log.d(TAG, "Text changed, refreshing view."); 
        refreshView(); 
       } 

       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) { 
       } 

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) { 
       } 
      }); 
} 
+2

Chỉ cần một lời bình luận: việc truyền tới một TextView là đủ. –

0

Dưới đây là mã nơi bạn có thể ghi đè lên actionbar copy/vv Paste

public class MainActivity extends Activity { 
    EditText editText; 
    private ClipboardManager myClipboard; 
    private ClipData myClip; 

    @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); 

     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(); 

        myClip = ClipData.newPlainText("text", text); 
        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: 
        // add your custom code to get cut functionality according 
        // to your requirement 
        return true; 
       case android.R.id.paste: 
        // add your custom code to get paste functionality according 
        // to your requirement 
        return true; 

       default: 
        break; 
       } 
       return false; 
      } 
     });   
    }  
} 
0

Bạn có thể đặt Listener Class:

public interface GoEditTextListener { 
void onUpdate(); 

}

Сreate tự lớp cho EditText:

public class GoEditText extends EditText 
{ 
    ArrayList<GoEditTextListener> listeners; 

    public GoEditText(Context context) 
    { 
     super(context); 
     listeners = new ArrayList<>(); 
    } 

    public GoEditText(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     listeners = new ArrayList<>(); 
    } 

    public GoEditText(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     listeners = new ArrayList<>(); 
    } 

    public void addListener(GoEditTextListener listener) { 
     try { 
      listeners.add(listener); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Here you can catch paste, copy and cut events 
    */ 
    @Override 
    public boolean onTextContextMenuItem(int id) { 
     boolean consumed = super.onTextContextMenuItem(id); 
     switch (id){ 
      case android.R.id.cut: 
       onTextCut(); 
       break; 
      case android.R.id.paste: 
       onTextPaste(); 
       break; 
      case android.R.id.copy: 
       onTextCopy(); 
     } 
     return consumed; 
    } 

    public void onTextCut(){ 
    } 

    public void onTextCopy(){ 
    } 

    /** 
    * adding listener for Paste for example 
    */ 
    public void onTextPaste(){ 
     for (GoEditTextListener listener : listeners) { 
      listener.onUpdate(); 
     } 
    } 
} 

xml:

<com.yourname.project.GoEditText 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/editText1"/> 

Và trong hoạt động của bạn:

private GoEditText editText1; 

editText1 = (GoEditText) findViewById(R.id.editText1); 

      editText1.addListener(new GoEditTextListener() { 
       @Override 
       public void onUpdate() { 
//here do what you want when text Pasted 
       } 
      }); 
Các vấn đề liên quan