[직접 풀어보기]
1
activitiy_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/second"
android:text="Second 액티비티"
android:checked="true"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/third"
android:text="Third 액티비티"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"
android:id="@+id/btnNewActivity"/>
</LinearLayout>
second.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"
android:id="@+id/btnReturn"/>
</LinearLayout>
third.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Activity"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"
android:id="@+id/btnReturn2"/>
</LinearLayout>
AndroidManifest.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cookandroid.project10_1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Project10_1">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" android:label="Second 액티비티"/>
<activity android:name=".ThirdActivity" android:label="Third 액티비티"/>
</application>
</manifest>
MainActivity.java
더보기
package com.cookandroid.project10_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
RadioButton second = (RadioButton) findViewById(R.id.second);
RadioButton third = (RadioButton) findViewById(R.id.third);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(third.isChecked()){
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
startActivity(intent);
} else{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
}
});
}
}
SecondActivity.java
더보기
package com.cookandroid.project10_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
ThirdActivity.java
더보기
package com.cookandroid.project10_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Button btnReturn = (Button) findViewById(R.id.btnReturn2);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
2
01
activitiy_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv1"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/acha"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv2"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/cchi"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv3"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/eevee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv4"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/haemu"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv5"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/lan"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv6"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/nuni"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv7"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/paeng"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv8"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pichu"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv9"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/pika"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/btnResult"
android:layout_weight="1"
android:text="투표 종료"/>
</LinearLayout>
result.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_vertical"
android:stretchColumns="0"
android:layout_marginHorizontal="10dp">
<TableRow>
<TextView
android:layout_span="2"
android:id="@+id/imgTitle"
android:layout_gravity="center"
android:textSize="20dp"
android:text="제목"/>
</TableRow>
<TableRow>
<ImageView
android:layout_span="2"
android:id="@+id/imgview"
android:src="@drawable/pika"/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv1"
android:layout_gravity="center_vertical"
android:text="그림1"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar1"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv2"
android:layout_gravity="center_vertical"
android:text="그림2"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar2"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv3"
android:layout_gravity="center_vertical"
android:text="그림3"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar3"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv4"
android:layout_gravity="center_vertical"
android:text="그림4"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar4"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv5"
android:layout_gravity="center_vertical"
android:text="그림5"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar5"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv6"
android:layout_gravity="center_vertical"
android:text="그림6"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar6"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv7"
android:layout_gravity="center_vertical"
android:text="그림7"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar7"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv8"
android:layout_gravity="center_vertical"
android:text="그림8"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar8"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv9"
android:layout_gravity="center_vertical"
android:text="그림9"
android:textSize="15dp"/>
<RatingBar
android:id="@+id/rbar9"
style="?android:attr/ratingBarStyleIndicator"
android:layout_gravity="right"></RatingBar>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnReturn"
android:layout_span="2"
android:text="돌아가기"/>
</TableRow>
</TableLayout>
MainActivity.java
더보기
package com.cookandroid.project10_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("포켓몬 선호도 투표");
final int voteCount[] = new int[9];
for (int i = 0; i<9; i++){ voteCount[i] = 0; }
ImageView image[] = new ImageView[9];
Integer imageId[] = {R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5, R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9};
final String imgName[] = {"아차모", "찌르성게", "이브이", "해무기", "란클루스", "누니머기", "팽도리", "피츄", "피카츄"};
for (int i=0; i<imageId.length; i++){
final int index;
index = i;
image[index] = (ImageView) findViewById(imageId[index]);
image[index].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
voteCount[index]++;
Toast.makeText(getApplicationContext(), imgName[index] + ": 총 "+voteCount[index]+ " 표", Toast.LENGTH_SHORT).show();
}
});
Button btnFinish = (Button) findViewById(R.id.btnResult);
btnFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra("VoteCount", voteCount);
intent.putExtra("ImageName", imgName);
startActivity(intent);
}
});
}
}
}
ResultActivity.java
더보기
package com.cookandroid.project10_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle("투표 결과");
Intent intent = getIntent();
int[] voteResult = intent.getIntArrayExtra("VoteCount");
String[] imageName = intent.getStringArrayExtra("ImageName");
int highest = 0;
TextView tv[] = new TextView[imageName.length];
RatingBar rbar[] = new RatingBar[imageName.length];
Integer tvID[] = {R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6, R.id.tv7, R.id.tv8, R.id.tv9};
Integer rbarID[] = {R.id.rbar1, R.id.rbar2, R.id.rbar3, R.id.rbar4, R.id.rbar5, R.id.rbar6, R.id.rbar7, R.id.rbar8, R.id.rbar9};
Integer imageFileID[] = {R.drawable.acha, R.drawable.cchi, R.drawable.eevee, R.drawable.haemu, R.drawable.lan, R.drawable.nuni, R.drawable.paeng, R.drawable.pichu, R.drawable.pika};
for(int i = 0; i<voteResult.length; i++){
tv[i] = (TextView) findViewById(tvID[i]);
rbar[i] = (RatingBar) findViewById(rbarID[i]);
}
for(int i = 0; i<voteResult.length; i++){
tv[i].setText(imageName[i]);
rbar[i].setRating((float) voteResult[i]);
if(voteResult[i]>voteResult[highest]){
highest = i;
}
}
TextView imgTitle = (TextView) findViewById(R.id.imgTitle);
ImageView imgView = (ImageView) findViewById(R.id.imgview);
imgTitle.setText(imageName[highest]);
imgView.setImageResource(imageFileID[highest]);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
3
activitiy_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtNum1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtNum2"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/plus"
android:checked="true"
android:text="더하기"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/minus"
android:text="빼기"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/multi"
android:text="곱하기"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/division"
android:text="나누기"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnNewActivity"
android:text="더하기"/>
</LinearLayout>
second.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnReturn"
android:text="돌아가기"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.a10_3;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");
Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
RadioButton plus, minus, multi, divi;
plus = (RadioButton) findViewById(R.id.plus);
minus = (RadioButton) findViewById(R.id.minus);
multi = (RadioButton) findViewById(R.id.multi);
divi = (RadioButton) findViewById(R.id.division);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edtNum1 = (EditText) findViewById(R.id.edtNum1);
EditText edtNum2 = (EditText) findViewById(R.id.edtNum2);
int simbol;
if(minus.isChecked()){simbol = 2;}
else if(multi.isChecked()){simbol = 3;}
else if(divi.isChecked()){simbol = 4;}
else{simbol=1;}
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Num1", Integer.parseInt(edtNum1.getText().toString()));
intent.putExtra("Num2", Integer.parseInt(edtNum2.getText().toString()));
intent.putExtra("Simbol", simbol);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
int hap = data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "답: " + hap, Toast.LENGTH_SHORT).show();
}
}
}
SecondActivity.java
더보기
package com.cookandroid.a10_3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("Second 액티비티");
int hapValue = 0;
final int result;
Intent inIntent = getIntent();
switch(inIntent.getIntExtra("Simbol", 0)){
case 1:
hapValue = inIntent.getIntExtra("Num1", 0)+inIntent.getIntExtra("Num2", 0);
break;
case 2:
hapValue = inIntent.getIntExtra("Num1", 0)-inIntent.getIntExtra("Num2", 0);
break;
case 3:
hapValue = inIntent.getIntExtra("Num1", 0)*inIntent.getIntExtra("Num2", 0);
break;
case 4:
hapValue = inIntent.getIntExtra("Num1", 0)/inIntent.getIntExtra("Num2", 0);
break;
}
result = hapValue;
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
outIntent.putExtra("Hap", result);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}
'DEVELOPMENT' 카테고리의 다른 글
[Android Studio] Chapter 11 직접 풀어보기 (0) | 2021.07.07 |
---|---|
[Android Studio] Chapter 10 연습문제 (0) | 2021.07.06 |
[Android Studio] Chapter 9 연습문제 (0) | 2021.07.05 |
[Android Studio] Chapter 9 직접 풀어보기 (0) | 2021.07.05 |
[Android Studio] Chapter 8 연습문제 (0) | 2021.07.02 |