how to enable select/copy texts in android?

允我心安 提交于 2019-12-10 11:51:56

问题


this is my app strategy:

  • there is a text view that contains texts.
  • for editing them, i define Action mode items(e.g. EDIT- this will texts of text view into edit text)although there are some default actions like select all/copy/.. as selecting text, so i don not want to define them.

i use this code for enabling the default selecting/copy buttons. but as i clicked the buttons, nothing happens. why??? xml code:

 <TextView
    android:id="@+id/speech"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:textSize="25sp" android:textAlignment="inherit"
    android:selectAllOnFocus="true"
    android:background="@drawable/curved_background"
    android:layout_centerInParent="true" android:textIsSelectable="true"
    android:padding="10dp"  />

initializing TextView in class:

 speech_text = (TextView) findViewById(R.id.speech);
 registerForContextMenu(speech_text);
    speech_text.setTextIsSelectable(true);
    speech_text.setCustomSelectionActionModeCallback(new SelectText());

SelectingText java class:

public class SelectText implements ActionMode.Callback {

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {


    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.text_select, menu);
    return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    //  Log.d(Log, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));

    switch (item.getItemId()) {

        case ... //other actions
            return true;
    }
    return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}
}

Note: as i control my code. the problem is in defining SelectText class in this line: speech_text.setCustomSelectionActionModeCallback(new SelectText()); because as i delete thiscode, every thing worksgood!


回答1:


You can make your text in app enable for copy/paste in clipboard by adding below line :

android:textIsSelectable

For more information, Please check below reference, http://developer.android.com/reference/android/widget/TextView.html

Thanks.



来源:https://stackoverflow.com/questions/37052973/how-to-enable-select-copy-texts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!