有人能告诉我如何复制在一个特定的文本视图到剪贴板,当一个按钮被按下?
@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 :)
}
});
}
我想复制文本在TextView TextView到剪贴板时,按钮bCopy按下。
Kotlin助手方法附加单击复制TextView上的文本
把这个方法放在Util类中的某个地方。这个方法附加点击监听器的textview复制内容的textview到一个clipText点击该textview
/**
* Param: cliplabel, textview, context
*/
fun attachClickToCopyText(textView: TextView?, clipLabel: String, context: Context?) {
if (textView != null && null != context) {
textView.setOnClickListener {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(clipLabel, textView!!.text)
clipboard.primaryClip = clip
Snackbar.make(textView,
"Copied $clipLabel", Snackbar.LENGTH_LONG).show()
}
}
}
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) DetailView.this
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("" + yourMessage.toString());
Toast.makeText(AppCstVar.getAppContext(),
"" + getResources().getString(R.string.txt_copiedtoclipboard),
Toast.LENGTH_SHORT).show();
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) DetailView.this
.getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData
.newPlainText("message", "" + yourMessage.toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(AppCstVar.getAppContext(),
"" + getResources().getString(R.string.txt_copiedtoclipboard),
Toast.LENGTH_SHORT).show();
}