7

我有一个编辑文本,我想在其中粘贴一些文本。我可以从某个网页复制文本,但无法将文本粘贴到我的 edittext 控件中。如何启用我的 edittext 来粘贴一些文本。这是我用于 edittext 的 main.xml ;

enter code here

<EditText 
   android:id="@+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="@color/black"
/>

谢谢

4

4 回答 4

8

这是在 Android 4.4.2 Samsung S4 上;

TextView的文档说:

To allow users to copy some or all of the TextView's value and paste it somewhere else, set the XML attribute android:textIsSelectable to "true" or call setTextIsSelectable(true). The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.

By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.

So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.

于 2014-06-11T12:41:28.230 回答
3

根据您的问题,如果您在系统中的任何位置复制了一些数据,并且您想将其粘贴到某个特定变量中,例如 Edit TextBox、Textview 等,那么这段代码肯定会对您有所帮助。

 ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
 myEdtTxt.setText(clipMan.getText());

注意:- 这里的 clipMan 对象将在复制过程发生时存储数据,我们将从该对象返回该数据并设置它,

于 2012-12-03T18:18:28.330 回答
3

To enable the standard copy/paste for TextView, U can choose one of the following: Change in layout file: Either add below property to your TextView

android:textIsSelectable="true"

and In your Java class write this line to set it programmatically.

myTextView.setTextIsSelectable(true);

if fragment try with

mContext.myTextView.setTextIsSelectable(true);

And long press on the TextView you can see copy/paste action bar.

于 2017-03-15T06:58:53.620 回答
0

尝试inputType="text"设置EditText字段

于 2012-05-15T04:08:13.827 回答