码迷,mamicode.com
首页 > 移动开发 > 详细

Android文字的复制和粘贴

时间:2015-04-01 13:14:12      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

Android中提供了简单的额复制粘贴功能。代码很简单

复制文字的代码:

ClipboardManager cbm= (ClipboardManager) MainActivity.this 
                .getSystemService(Context.CLIPBOARD_SERVICE); 
                 cbm.setText("要复制的文字"); 

粘贴文字的代码:

ClipboardManager paste = (ClipboardManager) MainActivity.this 
                .getSystemService(Context.CLIPBOARD_SERVICE); 
                String content=plaster.getText().toString(); 

但是在API11之后 复制粘贴是有差异的,其实就是导包的问题,详情参见http://www.apkbus.com/android-174549-1-1.html

这里粘上原文

在前面的项目中有做一个复制粘贴的功能,但是在api11 之后 复制粘贴是有差异的,而且网上的都只有一部分的, 现在分享给大家;
public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.top_back:
                        finish();
                        break;
                case R.id.btn_invited_copy:
                        int sdkInt = Build.VERSION.SDK_INT;
                        if (sdkInt > Build.VERSION_CODES.HONEYCOMB) {// api11
                                ClipboardManager copy = (ClipboardManager) ActMyInvitationCode.this
                                                .getSystemService(Context.CLIPBOARD_SERVICE);
                                copy.setText(invitationCode);
                                Toast.makeText(ActMyInvitationCode.this, "邀请码成功复制到粘贴板",
                                                Toast.LENGTH_SHORT).show();
                        } else if (sdkInt <= Build.VERSION_CODES.HONEYCOMB) {
                                android.text.ClipboardManager copyq = (android.text.ClipboardManager) ActMyInvitationCode.this
                                                .getSystemService(Context.CLIPBOARD_SERVICE);
                                copyq.setText(invitationCode);
                                Toast.makeText(ActMyInvitationCode.this, "邀请码成功复制到粘贴板",
                                                Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
        }
有的童鞋还没有看出其中的差别  在api 11之后用的是android.content.ClipboardManager;而在api11之前用的是android.text.ClipboardManager;

】  

Android文字的复制和粘贴

标签:

原文地址:http://www.cnblogs.com/dongweiq/p/4383478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!