Android 復(fù)制文本內(nèi)容到系統(tǒng)剪貼板的最簡單實(shí)例(分享)
這個例子很簡單,直接上截圖和代碼。

布局文件activity_copy.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="記者問一路人:“您覺得霧霾影響大嗎?”路人:“能不大嗎?首先你要看清楚。"
android:textSize="20sp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="onClickCopy"
android:text="復(fù)制上面的文本內(nèi)容" />
</LinearLayout>后臺CopyActivity.java代碼如下:
package chengyujia.demo.aty;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import chengyujia.demo.R;
public class CopyActivity extends BaseActivity {
private TextView tvMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy);
tvMsg = (TextView) findViewById(R.id.tvMsg);
}
public void onClickCopy(View v) {
// 從API11開始android推薦使用android.content.ClipboardManager
// 為了兼容低版本我們這里使用舊版的android.text.ClipboardManager,雖然提示deprecated,但不影響使用。
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 將文本內(nèi)容放到系統(tǒng)剪貼板里。
cm.setText(tvMsg.getText());
Toast.makeText(this, "復(fù)制成功,可以發(fā)給朋友們了。", Toast.LENGTH_LONG).show();
}
}核心代碼就兩句:
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(要復(fù)制的文本內(nèi)容);
以上這篇Android 復(fù)制文本內(nèi)容到系統(tǒng)剪貼板的最簡單實(shí)例(分享)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Flutter實(shí)現(xiàn)GIF動畫效果的方法詳解
如果我們想對某個組件實(shí)現(xiàn)一組動效應(yīng)該怎么辦呢?本文將利用Android Flutter實(shí)現(xiàn)GIF動畫效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
MVVMLight項(xiàng)目Model?View結(jié)構(gòu)及全局視圖模型注入器
這篇文章主要為大家介紹了MVVMLight項(xiàng)目中Model及View的結(jié)構(gòu)及全局視圖模型注入器的使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01
OpenGL中的glutInitDisplayMode()函數(shù)的理解
今天小編就為大家分享一篇關(guān)于OpenGL中的glutInitDisplayMode()函數(shù)的理解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
DownloadManager實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了DownloadManager實(shí)現(xiàn)文件下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

