首页 > 系统 > Android > 正文

Android剪贴板用法详解

2020-04-11 11:43:14
字体:
来源:转载
供稿:网友

本文实例详述了Android剪贴板的用法,分享给大家供大家参考。具体方法分析如下:

这里首先需要注意的一点,就是在使用Android剪贴板的时候大家只记住一点就行了,不管是安卓设备还是PC机,复制粘贴在同一时间里只能用于一个对象上,整通俗点就是:PC机上,不可能同时从C盘复制,又从D盘复制就行了,具体的看代码,很简单,直接上代码:
 

复制代码 代码如下:
package com.xiaoma.clipboard.demo;
 
 import android.app.Activity;
 import android.content.ClipData;
 import android.content.ClipData.Item;
 import android.content.ClipDescription;
 import android.content.ClipboardManager;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.Toast;
 
 /** 
 * @Title: ClipBoardDemoActivity.java
 * @Package com.xiaoma.clipboard.demo
 * @Description: 剪贴板学习
 * @author MZH
 */
 public class ClipBoardDemoActivity extends Activity implements OnClickListener{
    
     private Button put = null;
     private Button get = null;
     private ClipboardManager clipboard = null;
     private static final String CONTACTS = "content://com.example.contacts";
     private String COPY_PATH = "/copy";
     public static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.xiaoma.contact";
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         init();
     }
    
     /**
      * 初始化方法实现
      */
     private void init(){
         put = (Button)findViewById(R.id.button1);
         put.setOnClickListener(this);
        
         get = (Button)findViewById(R.id.button2);
         get.setOnClickListener(this);
     }
    
     /**
      * 监听实现
      */
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
         case R.id.button1:
             put();
             break;
         case R.id.button2:
             get();
             break;
         default:
             break;
         }
     }
    
     /**
      * 往Clip中放入数据
      */
     private void put(){
        
         /**
          * 往ClipboardManager中可放的数据类型有三种:
          * 因为大家都知道,就算是电脑,Ctrl+c也不可能在同一时间里即
          * 从C盘剪贴,又从D般剪贴,所以小马只写一种简单的信息进去,
          * 另外两种写在注释中
        
         //类型一:text
         clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
         ClipData textCd = ClipData.newPlainText("kkk", "WaHouHou!Clip....");
         clipboard.setPrimaryClip(textCd);
          */
         /**
          *
         //类型二:URI
         Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + "XiaoMa");
         ClipData clipUri = ClipData.newUri(getContentResolver(),"URI",copyUri);
         clipboard.setPrimaryClip(clipUri);
         *
         */
         //类型三:Intent
         //试下在Intent剪贴时使用Bundle传值进去
         clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
         Intent appIntent = new Intent();
         Bundle bundle = new Bundle();
         bundle.putInt("xiaoma", 3344258);
         bundle.putInt("yatou", 3344179);
         appIntent.putExtra("XiaoMaGuo", bundle);
         appIntent.setClass(ClipBoardDemoActivity.this, ReceiverClip.class);
         ClipData clipIntent = ClipData.newIntent("Intent",appIntent);
         clipboard.setPrimaryClip(clipIntent);
     }
    
     /**
      * 从Clip中取数据
      */
     private void get(){
         clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
         Item item = null;
        
         //无数据时直接返回
         if(!clipboard.hasPrimaryClip()){
             Toast.makeText(getApplicationContext(), "剪贴板中无数据", Toast.LENGTH_SHORT).show();
             return ;
         }
        
         //如果是文本信息
         if (clipboard.getPrimaryClipDescription().hasMimeType(
                 ClipDescription.MIMETYPE_TEXT_PLAIN)) {
             ClipData cdText = clipboard.getPrimaryClip();
             item = cdText.getItemAt(0);
             //此处是TEXT文本信息
             if(item.getText() == null){
                 Toast.makeText(getApplicationContext(), "剪贴板中无内容", Toast.LENGTH_SHORT).show();
                 return ;
             }else{
                 Toast.makeText(getApplicationContext(), item.getText(), Toast.LENGTH_SHORT).show();
             }
 
         //如果是INTENT
         } else if (clipboard.getPrimaryClipDescription().hasMimeType(
                 ClipDescription.MIMETYPE_TEXT_INTENT)) {
             //此处是INTENT
             item = clipboard.getPrimaryClip().getItemAt(0);
             Intent intent = item.getIntent();
             startActivity(intent);
             //........
        
         //如果是URI
         } else if (clipboard.getPrimaryClipDescription().hasMimeType(
                 ClipDescription.MIMETYPE_TEXT_URILIST)) {
             //此处是URI内容www.VeVB.COm
             ContentResolver cr = getContentResolver();
             ClipData cdUri = clipboard.getPrimaryClip();
             item = cdUri.getItemAt(0);
             Uri uri = item.getUri();
             if(uri != null){
                 String mimeType = cr.getType(uri);
                 if (mimeType != null) {
                     if (mimeType.equals(MIME_TYPE_CONTACT)) {
                         Cursor pasteCursor = cr.query(uri, null, null, null, null);
                         if (pasteCursor != null) {
                             if (pasteCursor.moveToFirst()) {
                                  //此处对数据进行操作就可以了,前提是有权限
                             }
                         }
                         pasteCursor.close();
                      }
                  }
             }
         }
     }
 }

下面是用来接收Intent传递的值,临时的一个Activity,代码更简单:
 

复制代码 代码如下:
package com.xiaoma.clipboard.demo;
 
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.TextureView;
 import android.widget.TextView;
 
 /**
  * @Title: ReceiverClip.java
  * @Package com.xiaoma.clipboard.demo
  * @Description:临时用来接收下从Clip传过来的Intent值
  * @author MZH
  */
 public class ReceiverClip extends Activity {
    
     private TextView tv1 ;
     private TextView tv2 ;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main2);
         init();
     }
    
     private void init(){
        
         tv1 = (TextView)findViewById(R.id.xiaoma);
         tv2 = (TextView)findViewById(R.id.yatou);
        
         Intent intent = getIntent();
         Bundle b =intent.getBundleExtra("XiaoMaGuo");
         if(b != null){
             int xiaoma = b.getInt("xiaoma");
             int yatou = b.getInt("yatou");
             if(!"".equals(String.valueOf(xiaoma)) && !"".equals(String.valueOf(yatou))){
                 tv1.setText(String.valueOf(xiaoma));
                 tv2.setText(String.valueOf(yatou));
             }
         }
     }
 }

全局配置文件里面没什么,如下:
 

复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android=""
     package="com.xiaoma.clipboard.demo"
     android:versionCode="1"
     android:versionName="1.0" >
 
     <uses-sdk android:minSdkVersion="14" />
 
     <application
         android:icon="@drawable/guoguo"
         android:label="@string/app_name" >
         <activity
             android:name=".ClipBoardDemoActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
         <activity android:name=".ReceiverClip"></activity>
     </application>
 
 </manifest>

希望本文所述对大家的Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表