Android在活动之间传递数据

时间:2016-04-17 08:21:37

标签: java android android-intent bundle

您好这是我的第一个问题所以请原谅我,如果格式不好,我的主要活动有三个按钮,按钮1和2导致数字选择器允许用户从数字中选择设置背景图像。我的问题是,一旦他们选择了颜色(rgb),我需要将其发送回主要活动但是我希望按钮立即改变为他们选择的背景颜色然后他们可以转到按钮2并执行相同的操作,在在过程结束时,button1和2都应该是他们选择的颜色。然后我将在按钮3中混合(我根本没有开始)我的主要问题是我选择一个然后它设置然后我选择另一个并且它onCreates()并将另一个设置回默认值,所以我不知何故需要在活动返回主活动后立即改变颜色我的代码有点难看但我只是试图让它工作然后我会回去让它更有效所以我道歉

//主要内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.MainActivity"
tools:showIn="@layout/activity_main">



<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mixButton"
    android:id="@+id/screen1MixerButton"
    android:layout_below="@+id/screen1Button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="103dp" />

<Button
    android:text="@string/Color1Text"
    android:layout_width="162dp"
    android:layout_height="188dp"
    android:id="@+id/screen1Button1"
    android:src="#ffffff"
    android:background="#ffffff"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
     />

<Button
    android:text="@string/Color2Text"
    android:layout_width="162dp"
    android:layout_height="188dp"
    android:id="@+id/screen1Button2"
    android:background="#ffffff"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignBottom="@+id/screen1Button1" />

</RelativeLayout>

// COLORRPICKER1 ACTIVITY

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker1"
tools:showIn="@layout/activity_color_picker1">

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/numberPicker1"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/numberPicker2"
    android:orientation="horizontal"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/numberPicker2"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:id="@+id/numberPicker3"></NumberPicker>



    <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="71dp"
        android:id="@+id/surfaceView1"
        android:layout_gravity="center_vertical"
        android:background="#000000"
        android:layout_below="@+id/numberPicker1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="99dp"></SurfaceView>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Color"
    android:id="@+id/PickColor1"
    android:layout_below="@+id/surfaceView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp" />

// COLORPICKER2活动     

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bignerdranch.android.colormixer.ColorPicker2"
tools:showIn="@layout/activity_color_picker2">

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/numberPicker1"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/numberPicker2"
    android:orientation="horizontal"></NumberPicker>

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/numberPicker2"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:id="@+id/numberPicker3"></NumberPicker>



    <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="71dp"
        android:id="@+id/surfaceView2"
        android:layout_gravity="center_vertical"
        android:background="#000000"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"></SurfaceView>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Color"
    android:id="@+id/PickColor2"
    android:layout_below="@+id/surfaceView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp" />


</RelativeLayout>

//主要活动

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;'

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MYTAG";
public static Button colorButton1;
private Button colorButton2;
private Button mixerButton;
int red1, green1, blue1,red2, green2, blue2 =255;
public static final int REQUEST_CODE= 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "in onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //START SETTING UP BUTTONS
    colorButton1 = (Button) findViewById(R.id.screen1Button1);
    colorButton2 = (Button) findViewById(R.id.screen1Button2);
    mixerButton = (Button) findViewById(R.id.screen1MixerButton);








    //ONCLICKLISTENER FOR MULTIPLE BUTTONS
     View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.screen1Button1:
                    Log.i(TAG, "screen1Button1 TAPPED");
                    Toast.makeText(MainActivity.this, "screen1Button1 TAPPED", Toast.LENGTH_LONG).show();
                    Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);

                    startActivityForResult(i1,REQUEST_CODE);

                    break;

                case R.id.screen1Button2:
                    Log.i(TAG, "screen1Button2 TAPPED");
                    Toast.makeText(MainActivity.this, "screen1Button2 TAPPED", Toast.LENGTH_LONG).show();
                    Intent i2 = new Intent(MainActivity.this,ColorPicker2.class);
                    startActivity(i2);
                    Log.i(TAG, "BACKFROM1");

                    break;

                case R.id.screen1MixerButton:
                    Log.i(TAG, "screen1MixerButton TAPPED");
                    Toast.makeText(MainActivity.this, "screen1MixerButton TAPPED", Toast.LENGTH_LONG).show();
                    Intent i3 = new Intent(MainActivity.this,MixedColorScreen.class);
                    startActivity(i3);

                    Log.i(TAG, "test value recieved" + red1);
                    Log.i(TAG, "test value recieved" + green1);
                    Log.i(TAG, "test value recieved" + blue1);
                    Log.i(TAG, "test value recieved" + red2);
                    Log.i(TAG, "test value recieved" + green2);
                    Log.i(TAG, "test value recieved" + blue2);
                    red1 = getIntent().getIntExtra("ColorPicker1_red",0);
                    green1 = getIntent().getIntExtra("ColorPicker1_green",0);
                    blue1 = getIntent().getIntExtra("ColorPicker1_blue",0);
                    red2 = getIntent().getIntExtra("ColorPicker2_red",0);
                    green2 = getIntent().getIntExtra("ColorPicker2_green",0);
                    blue2 = getIntent().getIntExtra("ColorPicker2_blue",0);
                    colorButton1.setBackgroundColor(Color.rgb(red1, green1, blue1));
                    colorButton2.setBackgroundColor(Color.rgb(red2, green2, blue2));

                    break;

            }

        }
    };

    colorButton1.setOnClickListener(onClickListener);
    colorButton2.setOnClickListener(onClickListener);
    mixerButton.setOnClickListener(onClickListener);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.i(TAG, "in onCreateOptionsMenu");
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(TAG, "in onOptionsItemsSelected");
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

// colorpicker1活动

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class ColorPicker1 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;
private Button pickColorButton1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_picker1);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    pickColorButton1 = (Button) findViewById(R.id.PickColor1);
    pickColorButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {;
            Intent colorPick1 = new Intent(ColorPicker1.this,MainActivity.class);
            colorPick1.putExtra("ColorPicker1_red",red);
            colorPick1.putExtra("ColorPicker1_green", green);
            colorPick1.putExtra("ColorPicker1_blue", blue);
            //TRY

            //MainActivity.colorButton1 = (Button) findViewById(R.id.screen1Button1);
            //colorPick1.setBackgroundColor(Color.rgb(red, green, blue));

            //END TRY
            Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red +green + blue);
            startActivity(colorPick1);
        }
    });

    //STARTING LOGS ----------------------------------------------------------------------------
    Log.i(TAG,"IN COLORPICKER1");
    Toast.makeText(ColorPicker1.this, "IN COLORPICKER1", Toast.LENGTH_LONG).show();


    //SETUP NUMBERPICKERS-----------------------------------------------------------------------
    np = (NumberPicker) findViewById(R.id.numberPicker1);
    np.setMinValue(0);
    np.setMaxValue(255);
    np.setWrapSelectorWheel(true);

    np2 = (NumberPicker) findViewById(R.id.numberPicker2);
    np2.setMinValue(0);
    np2.setMaxValue(255);
    np2.setWrapSelectorWheel(true);

    np3 = (NumberPicker) findViewById(R.id.numberPicker3);
    np3.setMinValue(0);
    np3.setMaxValue(255);
    np3.setWrapSelectorWheel(true);

    //VALUE CHANGED LISTENERS-------------------------------------------------------------------
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            red = np.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));

        }
    });

    np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            green = np2.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            blue = newVal;
            img = (SurfaceView) findViewById(R.id.surfaceView1);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    //CLOSE-------------------------------------------------------------------------------------

}

}

// colorpicker2活动

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class ColorPicker2 extends AppCompatActivity {
private static final String TAG = "MYTAG";
NumberPicker np, np2, np3;
SurfaceView img;
int red, green, blue ;//might not need
private Button pickColorButton2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_picker2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    pickColorButton2 = (Button) findViewById(R.id.PickColor2);
    pickColorButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ColorPicker2.this, "COlorPick2", Toast.LENGTH_LONG).show();
            Intent colorPick2 = new Intent(ColorPicker2.this,MainActivity.class);
            colorPick2.putExtra("ColorPicker2_red",red);
            colorPick2.putExtra("ColorPicker2_green", green);
            colorPick2.putExtra("ColorPicker2_blue", blue);

            Log.i(TAG, "PUTING IN PUTEXTRAS VALUES RED BLUE GREEN =" + red + green + blue);
            startActivity(colorPick2);
        }
    });

    //STARTING log -----------------------------------------------------------------------------
    Log.i(TAG, "IN COLORPICKER2");
    Toast.makeText(ColorPicker2.this, "IN COLOPICKER2", Toast.LENGTH_LONG).show();



    //SETTING UP NUMBER PICKERS ----------------------------------------------------------------
    np = (NumberPicker) findViewById(R.id.numberPicker1);
    np.setMinValue(0);
    np.setMaxValue(255);
    np.setWrapSelectorWheel(true);

    np2 = (NumberPicker) findViewById(R.id.numberPicker2);
    np2.setMinValue(0);
    np2.setMaxValue(255);
    np2.setWrapSelectorWheel(true);

    np3 = (NumberPicker) findViewById(R.id.numberPicker3);
    np3.setMinValue(0);
    np3.setMaxValue(255);
    np3.setWrapSelectorWheel(true);

    //SETTING UP LISTENERS ---------------------------------------------------------------------
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            red = np.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });

    np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            green = np2.getValue();
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            blue = newVal;
            img = (SurfaceView) findViewById(R.id.surfaceView2);
            img.setBackgroundColor(Color.rgb(red, green, blue));
        }
    });
    //end --------------------------------------------------------------------------------------
}

}

1 个答案:

答案 0 :(得分:0)

为此,每次打开startActivityForResult

时,都必须致电ColorPicker1/2/3

喜欢这个

    Intent i1 = new Intent(MainActivity.this,ColorPicker1.class);
                        startActivityForResult(i1,REQUEST_CODE_COlLOR1);
//REQUEST_CODE must be greater than`0` so keep it `101` for example

并且对于ColorPicker2,将REQUEST_CODE_COlLOR2保留为102,将ColorPicker3 REQUEST_CODE_COlLOR2保留为103

和你的ColorActivity1 / 2/3使用respective request codes

调用这样的意图
Intent i = new Intent();
i.putExtra("color1", yourColor);
setResult(REQUEST_CODE_COlLOR1, i);
finish();

用于第二种颜色活动

Intent i = new Intent();
i.putExtra("color2", yourColor);
setResult(REQUEST_CODE_COlLOR2, i);
finish();

在你的混合

Intent i = new Intent();
i.putExtra("color3", yourColor);
setResult(REQUEST_CODE_COlLOR3, i);
finish();

然后在OnActivityResult中编写MainActivity覆盖方法以从ColorPicker1 / 2/3获取结果

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_CODE_COlLOR1) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your first button
           yourColor =  data.getExtras().get("color1");
        }

    }
    if (requestCode == REQUEST_CODE_COlLOR2) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your second button
           yourColor2 =  data.getExtras().get("color2");
        }

    }
    if (requestCode == REQUEST_CODE_COlLOR3) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            //Set your color on your mixed color button
            yourColor3 =  data.getExtras().get("color3");
        }

    }
}