java 编辑文本的粘贴选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10593724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:44:12  来源:igfitidea点击:

paste option for edittext

javaandroid

提问by artist

I have an edittext and I would like to paste some text in it. 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 来粘贴一些文本。这是我用于 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"
/>

Thanks

谢谢

回答by Kerem

This is on Android 4.4.2 Samsung S4;

这是在 Android 4.4.2 三星 S4 上;

Documentation for TextViewsays 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:textIsSelectableto "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.

要允许用户复制部分或全部 TextView 的值并将其粘贴到其他位置,请将 XML 属性android:textIsSelectable 设置为“true”或调用setTextIsSelectable(true)。textIsSelectable 标志允许用户在 TextView 中做出选择手势,进而触发系统的内置复制/粘贴控件。

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

还有另一个名为android:cursorVisible 的 Textview 属性,它确定是否应该调用系统关于复制/粘贴回调。

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:inputTypewhich also did not make a difference for me.

默认情况下,我相信这两个都是正确的,并且已经启用了选择/复制/粘贴机制。我无法通过使用来改变这种行为,android:textIsSelectable="false"但如果我android:cursorVisible="false"最初设置,则无法在 EditText 中粘贴任何内容。只有在您输入内容后,光标和选择行为才会再次启用。也许这应该在代码中而不是在布局 xml 中处理,或者它可能与android:inputType这对我也没有影响有关。

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

因此android:cursorVisible="true",如果默认情况下未启用粘贴,请尝试在 EditText 的布局 xml 中进行设置。

回答by Tarit Ray

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 启用标准复制/粘贴,您可以选择以下选项之一: 布局文件更改:将以下属性添加到您的 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 您可以看到复制/粘贴操作栏。

回答by Pir Fahim Shah

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.

根据您的问题,如果您在系统中的任何位置复制了一些数据,并且想要将其粘贴到某个特定变量中,例如 Edit 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对象将在复制过程发生时存储数据,我们将从该对象返回该数据并设置它,

回答by dineth

Try setting the inputType="text"for the EditTextfield

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