如何在 Android 中为 TextView 启用标准复制粘贴?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10386084/
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
How do I enable standard copy paste for a TextView in Android?
提问by Ixx
I want to enable standard copy paste for a TextView (the same as for EditText). How can I do it?
我想为 TextView 启用标准复制粘贴(与 EditText 相同)。我该怎么做?
I tried using a non-editable EditText but it didn't work well (sometimes it became editable or the copy paste overlay was not shown). And it's probably not a good approach generally.
我尝试使用不可编辑的 EditText 但效果不佳(有时它变得可编辑或未显示复制粘贴叠加层)。这可能不是一个好的方法。
Need a working solution starting at API 7.
需要从 API 7 开始的可行解决方案。
采纳答案by Ixx
This works for copy pre-Honeycomb:
这适用于复制前蜂窝:
import android.text.ClipboardManager;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(textView.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
});
回答by CommonsWare
i.e., android:textIsSelectable="true"
IE, android:textIsSelectable="true"
回答by RamiReddy
To enable the standard copy/paste for TextView, U can choose one of the following:
要启用 TextView 的标准复制/粘贴,您可以选择以下选项之一:
Change in layout file: add below property to your TextView
android:textIsSelectable="true"
In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
布局文件中的更改:将以下属性添加到您的 TextView
android:textIsSelectable="true"
在您的 Java 类中编写这一行以编程方式设置它。
myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
长按 TextView 您可以看到复制/粘贴操作栏。
回答by AZ_
Requires API 11, Updated Code, previous method is deprecated
需要 API 11,更新代码,不推荐使用以前的方法
Solution for theme full screen withoutActionBar
无主题全屏解决方案ActionBar
Extend TextView
and in constructor paste following code
扩展TextView
并在构造函数中粘贴以下代码
this.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipboardManager cManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData cData = ClipData.newPlainText("text", getText());
cManager.setPrimaryClip(cData);
Util.toast(mContext, string.text_copyed);
return true;
}
});
回答by Agilanbu
In xml textview paste this code
在 xml textview 中粘贴此代码
android:textIsSelectable="true"
Then in java file,
然后在java文件中,
final TextView txtcopypaste = findViewById(R.id.txtcopypaste); // my textview
txtcopypaste.setOnClickListener(new View.OnClickListener() { // set onclick listener to my textview
@Override
public void onClick(View view) {
ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(txtcopypaste.getText().toString());
Toast.makeText(getApplicationContext(), "Copied :)", Toast.LENGTH_SHORT).show();
}
});
Requirement :Need to copy and paste the text which is in the textview.
要求:需要复制并粘贴textview中的文本。
OutCome :Using textview , once i clicked the textview. Its automatically copied the text which is in the textview.
结果:使用 textview ,一旦我点击了 textview 。它会自动复制文本视图中的文本。
Note:While importing clipboardmanager try to prefer
注意:在导入剪贴板管理器时尝试更喜欢
Please prefer text clipboard manager
请首选文本剪贴板管理器
import android.text.ClipboardManager; // prefer this
try to avoid content clipboard manager
尽量避免内容剪贴板管理器
import android.content.ClipboardManager; // Not this
回答by Michael Mao
use theme
@android:style/Theme.Black.NoTitleBar.Fullscreen
or
@android:style/Theme.WithActionBar
set TextView in xml
android:textIsSelectable="true"
see result
使用主题
@android:style/Theme.Black.NoTitleBar.Fullscreen
或者
@android:style/Theme.WithActionBar
在 xml 中设置 TextView
android:textIsSelectable="true"
查看结果
回答by narcis dpr
if someone wants to go the extra mile and do the select and copy to the clipboard with one click :
如果有人想加倍努力并一键选择并复制到剪贴板:
phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("PhoneNumber", phone.getText());
clipboard.setPrimaryClip(clip);
}
});
phone is the TextView and phone.Text is the Text that will be copied to the clipboard.
phone 是 TextView 和 phone.Text 是将被复制到剪贴板的文本。
回答by Beeing Jk
thisis better:
这是更好的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
final android.content.ClipData clipData = android.content.ClipData
.newPlainText("text label", "text to clip");
clipboardManager.setPrimaryClip(clipData);
} else {
final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("text to clip");
}
回答by rajeesh
For an EditText
,
in manifest inside the activity use
android:windowSoftInputMode="adjustResize"
对于EditText
, 在活动内的清单中使用
android:windowSoftInputMode="adjustResize"