Dar*_*wda 277 clipboard android copy-paste clipboard-interaction clipboardmanager
任何人都可以告诉我如何在按下按钮时将特定文本视图中的文本复制到剪贴板?Thanx :)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String getstring = textView.getText().toString();
//Help to continue :)
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
}
我想在按下按钮bCopy时将TextView textView中的Text复制到剪贴板!请帮忙......
sti*_*ike 520
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
Run Code Online (Sandbox Code Playgroud)
确保你已导入android.content.ClipboardManager而不是android.text.ClipboardManager.后者已被弃用.请查看此链接以获取更多信息.
vuh*_*990 65
这里是将文本复制到剪贴板的方法:
private void setClipboard(Context context, String text) {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
clipboard.setPrimaryClip(clip);
}
}
Run Code Online (Sandbox Code Playgroud)
此方法适用于所有Android设备.
A.S*_*.S. 57
昨天我上了这堂课.拿它来说,它适用于所有API级别
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.util.Log;
import de.lochmann.nsafirewall.R;
public class MyClipboardManager {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public boolean copyToClipboard(Context context, String text) {
try {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
.getSystemService(context.CLIPBOARD_SERVICE);
clipboard.setText(text);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
.getSystemService(context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText(
context.getResources().getString(
R.string.message), text);
clipboard.setPrimaryClip(clip);
}
return true;
} catch (Exception e) {
return false;
}
}
@SuppressLint("NewApi")
public String readFromClipboard(Context context) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
.getSystemService(context.CLIPBOARD_SERVICE);
return clipboard.getText().toString();
} else {
ClipboardManager clipboard = (ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
// Gets a content resolver instance
ContentResolver cr = context.getContentResolver();
// Gets the clipboard data from the clipboard
ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
String text = null;
String title = null;
// Gets the first item from the clipboard data
ClipData.Item item = clip.getItemAt(0);
// Tries to get the item's contents as a URI pointing to a note
Uri uri = item.getUri();
// If the contents of the clipboard wasn't a reference to a
// note, then
// this converts whatever it is to text.
if (text == null) {
text = coerceToText(context, item).toString();
}
return text;
}
}
return "";
}
@SuppressLint("NewApi")
public CharSequence coerceToText(Context context, ClipData.Item item) {
// If this Item has an explicit textual value, simply return that.
CharSequence text = item.getText();
if (text != null) {
return text;
}
// If this Item has a URI value, try using that.
Uri uri = item.getUri();
if (uri != null) {
// First see if the URI can be opened as a plain text stream
// (of any sub-type). If so, this is the best textual
// representation for it.
FileInputStream stream = null;
try {
// Ask for a stream of the desired type.
AssetFileDescriptor descr = context.getContentResolver()
.openTypedAssetFileDescriptor(uri, "text/*", null);
stream = descr.createInputStream();
InputStreamReader reader = new InputStreamReader(stream,
"UTF-8");
// Got it... copy the stream into a local string and return it.
StringBuilder builder = new StringBuilder(128);
char[] buffer = new char[8192];
int len;
while ((len = reader.read(buffer)) > 0) {
builder.append(buffer, 0, len);
}
return builder.toString();
} catch (FileNotFoundException e) {
// Unable to open content URI as text... not really an
// error, just something to ignore.
} catch (IOException e) {
// Something bad has happened.
Log.w("ClippedData", "Failure loading text", e);
return e.toString();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
// If we couldn't open the URI as a stream, then the URI itself
// probably serves fairly well as a textual representation.
return uri.toString();
}
// Finally, if all we have is an Intent, then we can just turn that
// into text. Not the most user-friendly thing, but it's something.
Intent intent = item.getIntent();
if (intent != null) {
return intent.toUri(Intent.URI_INTENT_SCHEME);
}
// Shouldn't get here, but just in case...
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
Tha*_*Zin 47
对于 Jetpack Compose
val localClipboardManager = LocalClipboardManager.current
localClipboardManager.setText(AnnotatedString("Your text here"))
Run Code Online (Sandbox Code Playgroud)
crg*_*dos 16
作为方便的Kotlin扩展:
fun Context.copyToClipboard(text: CharSequence){
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("label",text)
clipboard.primaryClip = clip
}
Run Code Online (Sandbox Code Playgroud)
更新:
如果使用ContextCompat,则应使用:
ContextCompat.getSystemService(this, ClipboardManager::class.java)
Run Code Online (Sandbox Code Playgroud)
Ekt*_*974 13
只需使用它.它只适用于android api> = 11之前你必须使用ClipData.
ClipboardManager _clipboard = (ClipboardManager) _activity.getSystemService(Context.CLIPBOARD_SERVICE);
_clipboard.setText(YOUR TEXT);
Run Code Online (Sandbox Code Playgroud)
希望它能帮助你:)
[更新2015年3月19日]就像Ujjwal Singh说的那样,这个方法setText现在已经弃用了,你应该使用,正如文档推荐的那样,setPrimaryClip(clipData)
Mer*_*n E 12
fun Context.copyToClipboard(text: CharSequence){
val clipboard = ContextCompat.getSystemService(this,ClipboardManager::class.java)
clipboard?.setPrimaryClip(ClipData.newPlainText("",text))
}
Run Code Online (Sandbox Code Playgroud)
小智 7
使用此代码
private ClipboardManager myClipboard;
private ClipData myClip;
TextView textView;
Button copyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = textView.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
这可以在Kotlin中完成,如下所示:
var clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
var clip = ClipData.newPlainText("label", file.readText())
clipboard.primaryClip = clip
Run Code Online (Sandbox Code Playgroud)
file.readText()你的输入字符串在哪里.
@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
@SuppressWarnings("deprecation")
@TargetApi(11)
public void onClickCopy(View v) { // User-defined onClick Listener
int sdk_Version = android.os.Build.VERSION.SDK_INT;
if(sdk_Version < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(textView.getText().toString()); // Assuming that you are copying the text from a TextView
Toast.makeText(getApplicationContext(), "Copied to Clipboard!", Toast.LENGTH_SHORT).show();
}
else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Text Label", textView.getText().toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to Clipboard!", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
使用此功能复制到剪贴板
public void copyToClipboard(String copyText) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(copyText);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText("Your OTP", copyText);
clipboard.setPrimaryClip(clip);
}
Toast toast = Toast.makeText(getApplicationContext(),
"Your OTP is copied", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 50, 50);
toast.show();
//displayAlert("Your OTP is copied");
}
Run Code Online (Sandbox Code Playgroud)
如果你想从 edittext 复制文本,那么首先创建 Edittext
EditText mResultEt = findViewById(R.id.resultEt);
Run Code Online (Sandbox Code Playgroud)
然后创建一个按钮,单击后我们可以复制这些文本
ImageButton copyClipBoard = findViewById(R.id.btn_copy);
Run Code Online (Sandbox Code Playgroud)
然后使用按钮的监听器
爪哇
copyClipBoard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboardManager = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("nonsense_data",
mResultEt.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(MainActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud)
科特林
btn1.setOnClickListener{
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(
"nonsense_data",
content_et.getText().toString()
)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(this@MainActivity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()
}
Run Code Online (Sandbox Code Playgroud)
并确保导入这个
import android.content.ClipboardManager;
Run Code Online (Sandbox Code Playgroud)
不要导入这个
android.text.ClipboardManager
Run Code Online (Sandbox Code Playgroud)