Est-il possible d'envoyer une commande passée afin qu'elle colle le texte dans le texte d'édition actuellement concentré. Scénario:
Je sais copier du texte avec ClipboardManager
, mais je ne sais pas comment le coller.
vous pouvez copier et coller du texte en utilisant le code suivant:
pour la copie:
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("your_text_to_be_copied");
clipboard.setPrimaryClip(clip);
Et collez-le:
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String pasteData = "";
// If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {
} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
// since the clipboard has data but it is not plain text
} else {
//since the clipboard contains plain text.
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
// Gets the clipboard as text.
pasteData = item.getText().toString();
}
pour plus de détails, vérifiez ici
Si vous souhaitez simplement "copier et coller" du code dans votre application, vous pouvez utiliser ce qui suit.
String textToCopy = etCodeWindow.getText().toString();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(null, textToCopy);
if (clipboard == null) return;
clipboard.setPrimaryClip(clip);
Obtenez le texte à coller
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard == null) return;
ClipData clip = clipboard.getPrimaryClip();
if (clip == null) return;
ClipData.Item item = clip.getItemAt(0);
if (item == null) return;
CharSequence textToPaste = item.getText();
if (textToPaste == null) return;
ou
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
try {
CharSequence textToPaste = clipboard.getPrimaryClip().getItemAt(0).getText();
} catch (Exception e) {
return;
}
ou la même chose dans Kotlin:
val clipboard: ClipboardManager = (getSystemService(Context.CLIPBOARD_SERVICE)
?: return false) as ClipboardManager
val clip = clipboard.primaryClip
?: return false
val item = clip.getItemAt(0)
?: return false
val textToPaste = item.text
?: return false
en l'insérant à la position du curseur
S'il y a une sélection, la sélection sera remplacée par le texte collé.
int start = Math.max(myEditText.getSelectionStart(), 0);
int end = Math.max(myEditText.getSelectionEnd(), 0);
myEditText.getText().replace(Math.min(start, end), Math.max(start, end),
textToPaste, 0, textToPaste.length());
Android.content.ClipboardManager
et Android.content.ClipData
.un bref résumé de ce qui précède après nid d'abeille> = API 13:
public String readFromClipboard() {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip()) {
Android.content.ClipDescription description = clipboard.getPrimaryClipDescription();
Android.content.ClipData data = clipboard.getPrimaryClip();
if (data != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
return String.valueOf(data.getItemAt(0).getText());
}
return null;
}
Je le fais de cette façon. Gestionnaire de presse-papiers pour tous les niveaux de l'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;
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 "";
}
}
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String copyedText = clipboard.getText();
Pour un moyen simple et efficace de copier-coller par programmation est ...
Créez un bouton et copiez ce code dans onclicklistener.
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Pour copie
clipboard.setText("which you want to copy");
Pour la pâte
textview1.setText(clipboard.getText().toString());