Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android剪貼板用法詳解

Android剪貼板用法詳解

編輯:關於Android編程

本文實例詳述了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.jb51.net
             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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved