select+在TextView中复制文本?

有没有办法让用户select/复制TextView中的文本? 我需要EditText的相同function,您可以长按控件并获得全选/复制的popup选项,但我需要控件看起来像一个TextView。

尝试了一些东西,如使EditText使用editable =“none”选项或inputType =“none”,但这些仍然保留了EditText的框架背景,我不想要,

谢谢

——-更新———————-

这是99%,所有我想要的是selectHILIGHT是可见的(橙色的东西)。 除此之外,这是好的,可以住这个虽然:

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" style="?android:attr/textViewStyle" android:textColor="@color/white" android:textAppearance="@android:style/TextAppearance.Medium" android:cursorVisible="false" android:background="@null" /> 

我想这是因为cursorVisible =“false”引起的,但是没有光标存在,即使没有做任何select。

android:textIsSelectable的作品(至less在ICS – 我还没有签入早期版本)

 <TextView android:id="@+id/deviceIdTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:text="" /> 

文本视图需要启用,focusable,longClickable和textIsSelectable

  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:id="@+id/pwTextView" android:enabled="true" android:textIsSelectable="true" android:focusable="true" android:longClickable="true" /> 

我想我有一个更好的解决scheme。 打电话
registerForContextMenu(yourTextView);

并且您的TextView将被注册为接收上下文菜单事件。

然后在Activity覆盖onCreateContextMenu

 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //user has long pressed your TextView menu.add(0, v.getId(), 0, "text that you want to show in the context menu - I use simply Copy"); //cast the received View to TextView so that you can get its text TextView yourTextView = (TextView) v; //place your TextView's text in clipboard ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(yourTextView.getText()); } 

希望这可以帮助你和其他人寻找一种从TextView复制文本的方法

我试图实现相同的,你的问题帮助我正确地设置我的editext布局。 那谢谢啦! 🙂

然后我意识到,如果光标打开,突出显示实际上是可见的。 但是我只是喜欢你不想看到光标,所以我将光标隐藏在layout.xml文件中,就像你一样,添加了一个eventlistener用于长时间的点击,只有当select开始时才显示光标。

因此,将监听器添加到onCreate部分的Activity中:

 public TextView htmltextview; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ... htmltextview.setOnLongClickListener(new OnLongClickListener(){ public boolean onLongClick(View v) { htmltextview.setCursorVisible(true); return false; } }); } 

而且,在开始时没有光标,如果你长时间点击,则光标出现在select边界上。

我希望我能帮上忙。

欢呼声,fm

我也试图做类似的事情,但仍然需要一个自定义的方法来处理TextView中的文本突出显示。 我触发了突出显示和复制LongClick行动。

这是我如何使用SpannableStringpipe理的:

 SpannableString highlightString = new SpannableString(textView.getText()); highlightString.setSpan(new BackgroundColorSpan(ContextCompat.getColor(getActivity(), R.color.gray)) , 0, textView.getText().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); textView.setText(highlightString); copyToClipboard(urlToShare); 

和复制function:

 public void copyToClipboard(String copyText) { ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("url", copyText); clipboard.setPrimaryClip(clip); Toast toast = Toast.makeText(getActivity(), "Link is copied", Toast.LENGTH_SHORT); toast.show(); } 

我希望这有助于最终解决这个问题的人:)

只需使用这个简单的库: GitHub:可选的TextView