建立两个Activity,当点击屏幕的时候,调到另外一个Activity,设置屏幕颜色,彼此来回切换。
新建BinderData.java类
package com.android.testrecord;
import android.os.Binder;
/**
* Created by wang on 16-11-1.
*/
public class BinderData extends Binder {
private int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
新建MyColor.java 类
package com.android.testrecord;
import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by wang on 16-11-1.
*/
public class MyColor implements Parcelable{
private BinderData data = new BinderData();
public MyColor() {
data.setColor(Color.BLACK);
}
protected MyColor(Parcel in) {
data = (BinderData) in.readValue(BinderData.class.getClassLoader());
}
public int getColor() {
return data.getColor();
}
public void setColor(int color) {
data.setColor(color);
}
public static final Creator<MyColor> CREATOR = new Creator<MyColor>() {
@Override
public MyColor createFromParcel(Parcel in) {
return new MyColor(in);
}
@Override
public MyColor[] newArray(int size) {
return new MyColor[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(data);
}
}
建立两个Activity,分别命名为MainActivity.java和SubActivity.java
package com.android.testrecord;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private final int SUB_ACTIVITY = 0;
private MyColor color = new MyColor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SUB_ACTIVITY) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("MyColor")) {
// color = data.getParcelableExtra("MyColor");
findViewById(R.id.text).setBackgroundColor(color.getColor());
}
}
}
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent();
intent.setClass(this, SubActivity.class);
color.setColor(Color.RED);
intent.putExtra("MyColor", color);
startActivityForResult(intent, SUB_ACTIVITY);
}
return super.onTouchEvent(event);
}
}
package com.android.testrecord;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
/**
* Created by wang on 16-11-1.
*/
public class SubActivity extends Activity{
private MyColor color;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.text)).setText("SubActivity");
Intent intent = getIntent();
if (intent != null) {
if (intent.hasExtra("MyColor")) {
color = intent.getParcelableExtra("MyColor");
findViewById(R.id.text).setBackgroundColor(color.getColor());
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent();
if (color != null) {
color.setColor(Color.GREEN);
intent.putExtra("MyColor", color);
}
setResult(RESULT_OK, intent);
finish();
}
return super.onTouchEvent(event);
}
}
建立资源布局文件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"
tools:context="com.android.comeon.MainActivity">
<TextView
android:id="@+id/text"
android:textSize="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="MainActivity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
注意需要在AndroidManifest.xml文件中添加
<activity android:name=".SubActivity">
</activity>
输出: