2016-04-17 7 views
0

こんにちはこれは私の最初の質問ですので、悪いフォーマットであれば私は3つのボタンでメインアクティビティを持っています。ボタン1と2は数字をピックアップして数字から選択して背景画像。私の問題は、一度色を選択すると(rgb)、それはメインのアクティビティに戻ってきますが、ボタンをすぐに背景色に変えてボタン2に行き、同じことをすることができます。ボタン1とボタン2の両方が選択された色でなければなりません。次に、ボタン3でミックスします(私は全く始まっていません)。私の主な問題は、1つを選択して設定した後、もう1つを選んでonCreates()を設定し、もう一方をデフォルトに戻します。どういうわけか、アクティビティがメインアクティビティに戻った後に色を変更する必要があります。私のコードはちょっと醜いですが、ちょうどそれを働かせようとしていました。私は戻ってより効率的にするつもりでした。アクティビティ間でデータをやりとりする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活動

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

これを見るから結果を得るためにMainActivityOnActivityResultオーバーライドメソッドを書きます://stackoverflow.com/a/7325248/1263362 – Sayem

答えて

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 

と103

として、あなたColorActivity1にColorPicker3 REQUEST_CODE_COlLOR2 102とし、ためREQUEST_CODE_COlLOR2を保つColorPicker2ため

/2/ respective request codes

Intent i = new Intent(); 
i.putExtra("color1", yourColor); 
setResult(REQUEST_CODE_COlLOR1, i); 
finish(); 

fのこのような3コールの意図http:または、あなたの混合

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

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

セカンドカラー活動にと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

関連する問題