• 通过剪切板传递数据—— 传递一个对象


    1、三个类

    MainActivity

    package com.example.app.jianqiebanchuanzhi;
    
    import android.content.ClipboardManager;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Base64;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectOutputStream;
    
    public class MainActivity extends AppCompatActivity {
    
        private Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button)this.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //从android系统中调用剪切板的服务
                    //ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                    //String name= "皇子-嘉文四世";
                    //clipboardManager.setText(name);
                   // Intent intent = new Intent(MainActivity.this,OtherActivity.class);
                    //startActivity(intent);
                    MyData myData = new MyData("jack",23);
                    //将对象转换成字符串
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    String base64String = "";
                    try {
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                        objectOutputStream.writeObject(myData);
                        base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
                        objectOutputStream.close();
                    }catch (Exception e){
    
                    }
                    ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboardManager.setText(base64String);
                    Intent intent = new Intent(MainActivity.this,OtherActivity.class);
                    startActivity(intent);
                }
            });
        }
    }

    MyData

    package com.example.app.jianqiebanchuanzhi;
    
    import java.io.Serializable;
    
    /**
     * Created by honghong on 2016/6/18.
     * Serializable 可序列花
     */
    public class MyData implements Serializable {
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    
        private String name;
        private int age;
    
        public String toString(){
            return "MyData[name="+name+",age="+age+"]";
        }
    
        public MyData(String name,int age){
            this.name =name;
            this.age = age;
        }
    
    
    }

    OtherActivity

    package com.example.app.jianqiebanchuanzhi;
    
    import android.app.Activity;
    import android.content.ClipboardManager;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Base64;
    import android.widget.TextView;
    
    import java.io.ByteArrayInputStream;
    import java.io.ObjectInputStream;
    
    /**
     * Created by honghong on 2016/6/18.
     */
    public class OtherActivity extends Activity{
    
        private TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.other);
            textView = (TextView)this.findViewById(R.id.Msg);
            ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
            String msgString = clipboardManager.getText().toString();
    
            //还原
            byte[] base64_byte = Base64.decode(msgString,Base64.DEFAULT);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte);
            try{
                ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
                MyData myData = (MyData)objectInputStream.readObject();
                textView.setText(myData.toString());
            }catch (Exception e){}
    
    
        }
    }

    2、Layout 下面布局

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.app.jianqiebanchuanzhi.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textView" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剪切板传递数据"
            android:id="@+id/button"
            android:layout_below="@+id/textView"
            android:layout_alignParentStart="true"
            android:layout_marginTop="55dp" />
    
    
    </RelativeLayout>

    other.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">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Msg"/>
    </LinearLayout>

    3、注册

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.app.jianqiebanchuanzhi">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".OtherActivity">
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    MySQL8.0.x免安装配置
    Java中锁的实现与内存语义
    并发中的volatile
    「LOJ #2163」「POI2011」Tree Rotations
    「CodeChef REBXOR」Nikitosh and xor
    「Codeforces 429D」Destiny
    「Luogu P2042」「NOI2005」维护数列
    「SPOJ SEQ」 Recursive Sequence
    「GCJ 2008 Round 1A C」numbers
    「LOJ #6016」崂山白花蛇草水
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/5596060.html
Copyright © 2020-2023  润新知