2013-02-26 38 views
7

मैं ActionMode.Callback उपयोग कर रहा हूँ में चयनित पाठ पर एक श्रोता स्थापित करने के लिए है, लेकिन मुझे लगता है मैं आपको लगता है पता करने के लिए जब पाठ का चयन किया जा रहा समाप्त हो गया है की जरूरत है ... उदाहरण केकैसे TextView

enter image description here

उत्तर

2

के लिए यहां अपना उत्तर पा सकते हैं:

Android Text Selection Listener

मुख्य शब्द को तुम यहाँ के लिए देख रहे हैं, अपने शोध में आपकी मदद करने, ०१२३४७३९६८ है, बशर्ते आपका लक्ष्य हनीकोम्ब या नया हो।

API docs ("प्रासंगिक कार्रवाई मोड का उपयोग करने के लिए नीचे स्क्रॉल करें) एक बार आप पाते हैं क्या आप के लिए देख रहे हैं, जो उनके उपयोग के सबसे बड़ी बाधा है समझा चीजों में से एक ठीक काम करते हैं, लेकिन मूल रूप से क्या

  1. अपने EditText सेट चयन होने के लिए (android:textIsSelectable="true" या setTextIsSelectable(true);
  2. लागू ActionMode.Callback इंटरफेस और अपने खुद के मेनू आइटम प्रदान
  3. : आप सब करने की ज़रूरत के लिए जा रहे है।

नोट: जैसा ऊपर बताया गया है, यह केवल एपीआई स्तर 11+ के लिए काम करता है। यदि आप पहले प्लेटफॉर्म को लक्षित कर रहे हैं, तो पाठ चयन के लिए ईवेंट प्राप्त करना अधिक जटिल है।

.xml में
1

:

textview.setCustomSelectionActionModeCallback(new callback(textview)); 
... 
public class callback implements Callback { 

    private TextView mTextView; 

    public callback(TextView text) { 
     this.mTextView = text; 

    } 

    @Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     int start = mTextView.getSelectionStart(); 
     int end = mTextView.getSelectionEnd(); 
     Spannable wordtoSpan = (Spannable) mTextView.getText(); 

     switch (item.getItemId()) { 

     case R.id.item_blue: 
      wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), start 
        , end, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

     singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.BLUE); 
      return true; 

     case R.id.item_green: 
      wordtoSpan.setSpan(new BackgroundColorSpan(Color.GREEN), start, end, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.GREEN); 
      return true; 

     case R.id.item_red: 
      wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), start, end, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.RED); 
      return true; 
     case R.id.item_yellow: 
      wordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.YELLOW); 
      return true; 
     case R.id.item_erase: 
      wordtoSpan.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), start, end, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.TRANSPARENT); 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
     mode.setTitle("Selecione a cor"); 
     mode.getMenuInflater().inflate(R.menu.menu_text_context, menu); 

     return true; 
    } 

    @Override 
    public void onDestroyActionMode(ActionMode mode) { 

    } 

    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     menu.removeItem(android.R.id.selectAll); 
     // Remove the "cut" option 
     menu.removeItem(android.R.id.cut); 
     // Remove the "copy all" option 
     menu.removeItem(android.R.id.copy); 
     return true; 
    } 

} 
:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textIsSelectable="true" /> 
.class में