MainActivity
package com.example.example_6_3;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ActivityResultLauncher<Intent> launcher;
int drawableID[] = {
R.drawable.shape01,
R.drawable.shape02,
R.drawable.shape03,
R.drawable.shape04,
R.drawable.shape05,
R.drawable.shape06,
};
int imageviewID[] = {
R.id.image01,
R.id.image02
};
RadioButton rdObj1;
RadioButton rdObj2;
ImageView ivObj[] = new ImageView[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rdObj1 = findViewById(R.id.radioButton1);
rdObj2 = findViewById(R.id.radioButton2);
LinearLayout layoutResultOjb = findViewById(R.id.layoutResult);
for (int i = 0; i<=1; i++)
ivObj[i] = findViewById(imageviewID[i]);
ImageButton btnObj = findViewById(R.id.imageButton);
btnObj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), subActivity.class);
if (rdObj1.isChecked() == true)
intent.putExtra("shape",1);
else if (rdObj2.isChecked() == true)
intent.putExtra("shape",2);
launcher.launch(intent);
}
});
launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
int resId[] = new int[6];
resId = data.getIntArrayExtra("answer");
int num = 0;
for (int i = 0; i<=5; i++) {
if (resId[i] >= 1) {
ivObj[num].setImageResource(drawableID[i]);
num++;
}
}
boolean flag = true;
for (int i = 0; i<=5; i++) {
if (rdObj1.isChecked()) {
if (resId[i] == 1 && i>2) flag = false;
}
else if (rdObj2.isChecked()) {
if ((resId[i] == 1 && i<=2) || (resId[i] == 1 && i >= 3)) flag = false;
}
layoutResultOjb.setVisibility(View.VISIBLE);
TextView tvObj = findViewById(R.id.textViewResult);
if (flag)tvObj.setText("맞았습니다");
else tvObj.setText("틀렸습니다");
}
}
}
});
}
}
subActivity
package com.example.example_6_3;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class subActivity extends AppCompatActivity {
int imageviewID[] = {
R.id.image01,
R.id.image02,
R.id.image03,
R.id.image04,
R.id.image05,
R.id.image06,
};
int framelayoutId[] = {
R.id.frame_layout01,
R.id.frame_layout02,
R.id.frame_layout03,
R.id.frame_layout04,
R.id.frame_layout05,
R.id.frame_layout06
};
ImageView ivObj[] = new ImageView[6];
FrameLayout flayoutObj[] = new FrameLayout[6];
ImageView imgArray[] = new ImageView[6];
int selectNum[] = new int[6];
int mShape = 0;
int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
Intent intent = getIntent();
mShape = intent.getIntExtra("shape",0);
ImageView shapeObj = findViewById(R.id.imageView);
if (mShape==1)
shapeObj.setImageResource(R.drawable.circle);
else if (mShape==2)
shapeObj.setImageResource((R.drawable.square));
for (int j=0; j<=5; j++) {
selectNum[j] = 0;
ivObj[j] = findViewById(imageviewID[j]);
flayoutObj[j] = findViewById(framelayoutId[j]);
}
for (int m=0; m<=5; m++) {
int i = m;
ivObj[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectNum[i]==1) {
selectNum[i] = 0;
--num;
flayoutObj[i].removeView(imgArray[i]);
}
else {
if (num < 2) {
selectNum[i] = 1;
num++;
imgArray[i] = new ImageView(getApplicationContext());
imgArray[i].setImageResource(R.drawable.ok_check);
flayoutObj[i].addView(imgArray[i]);
}
else Toast.makeText(getApplicationContext(),"2개 까지만 선택 가능합니다.", Toast.LENGTH_SHORT).show();
}
}
});
}
ImageButton btnObj = findViewById(R.id.imageButton);
btnObj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
intent.putExtra("answer", selectNum);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
activity_main
<LinearLayout 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:orientation="vertical"
android:padding="30dp">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="같은 모양 찾기 게임"
android:textColor="#8BC34A"
android:textSize="25sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="모양을 선택하세요"
android:textColor="#EA3DE4"
android:textSize="18sp"
android:textStyle="bold|italic"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="@drawable/circle" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
app:srcCompat="@drawable/square" />
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CIRCLE"
android:textColor="#0000FF"
android:textSize="24sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SQUARE"
android:textColor="#FF0000"
android:textSize="24sp"
android:textStyle="bold" />
</RadioGroup>
<LinearLayout
android:id="@+id/layoutResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_weight="1.8"
android:background="#AEEFF8"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/image01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/image02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="결과"
android:textColor="#F44336"
android:textSize="16sp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@android:color/transparent"
android:scaleType="centerInside"
app:srcCompat="@drawable/button" />
</LinearLayout>
activity_sub
<LinearLayout 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:layout_margin="20dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7"
android:gravity="right"
android:text="Matching Shape?"
android:textColor="#8BC34A"
android:textSize="25sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/circle" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<FrameLayout
android:id="@+id/frame_layout06"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image06"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape06" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame_layout03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape03" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame_layout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape01" />
</FrameLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<FrameLayout
android:id="@+id/frame_layout04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape04" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame_layout02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape02" />
</FrameLayout>
<FrameLayout
android:id="@+id/frame_layout05"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image05"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="@drawable/shape05" />
</FrameLayout>
</TableRow>
</TableLayout>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.6"
android:background="@android:color/transparent"
android:scaleType="centerInside"
app:srcCompat="@drawable/button2" />
</LinearLayout>