简体   繁体   English

编辑文本的粘贴选项

[英]paste option for edittext

I have an edittext and I would like to paste some text in it. 我有一个edittext,我想在其中粘贴一些文本。 I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ; 我可以从某个网页复制文本,但是我无法将文本粘贴到我的edittext控件中。如何启用我的edittext来粘贴一些文本。这是我的edit.xml的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"
/>

Thanks 谢谢

This is on Android 4.4.2 Samsung S4; 这是Android 4.4.2三星S4;

Documentation for TextView says that: 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) . 要允许用户复制部分或全部TextView的值并将其粘贴到其他位置,请将XML属性android:textIsSelectable设置为“true”或调用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. textIsSelectable标志允许用户在TextView中制作选择手势,进而触发系统的内置复制/粘贴控件。

There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks. 还有另一个名为android:cursorVisible的Textview attribure ,它确定是否应该调用关于复制/粘贴回调的系统。

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. 我无法通过使用android:textIsSelectable="false"来改变这种行为,但如果我最初设置android:cursorVisible="false" ,则无法在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. 也许这应该在代码中而不是在布局xmls中处理,或者它可能与android:inputType有关,这对我来说也没有什么不同。

So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default. 因此,如果默认情况下未启用粘贴,请尝试在EditText的布局xml中设置android:cursorVisible="true"

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 要为TextView启用标准复制/粘贴,U可以选择以下选项之一:更改布局文件:将以下属性添加到TextView

android:textIsSelectable="true"

and In your Java class write this line to set it programmatically. 在Java类中编写此行以编程方式设置它。

myTextView.setTextIsSelectable(true);

if fragment try with 如果片段尝试

mContext.myTextView.setTextIsSelectable(true);

And long press on the TextView you can see copy/paste action bar. 长按TextView,您可以看到复制/粘贴操作栏。

According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you. 根据您的问题,如果您在系统中的任何位置复制了一些数据,并且想要将其粘贴到某些特定变量中,例如编辑TextBox,Textview等,那么此代码肯定会对您有所帮助。

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

Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it, 注意: - 这里clipMan对象将在复制过程发生时存储数据,我们将从该对象返回该数据并进行设置,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM