2016-04-17 172 views
0

您好,这是我的第一个问题,所以请原谅我,如果它的格式不好,我有三个按钮的主要活动,按钮1和2导致numberpickers,允许用户从数字中选择设置背景图。我的问题是一旦他们选择了颜色(RGB),我需要它发回主要活动,但我希望按钮立即改变为他们选择的背景颜色,然后他们可以去按钮二,做同样的事情,在该过程结束时button1和2应该是他们选择的颜色。我将然后混合按钮3(我一直没有开始)我的主要问题是我选择一个然后它设置,但然后我选择另一个,它onCreates()并将其他设置为默认,所以我不知何故需要immedietly改变颜色的活动返回到主活动我的代码是丑了一点,但之后我刚一直在努力得到它的工作的话,我本来打算回去,使之更有效率,所以我的道歉Android在活动之间传递数据

//主要内容

<?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 -------------------------------------------------------------------------------------- 
} 

} 
+0

看到这一点:HTTP ://stackoverflow.com/a/7325248/1263362 – Sayem

回答

0

对于这一点,你必须每次当你打开时间打电话给startActivityForResultColorPicker1/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(); 

f或在二色活动

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

MainActivity您的混合

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

,然后写OnActivityResult覆盖方法从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"); 
     } 

    } 
} 
+0

谢谢,你看我不知道setResult有一个覆盖方法,这有助于谢谢你。 –

+0

欢迎您。很高兴帮助。 – Shishram

相关问题