728x90
한빛아카데미 Android Studio를 활용한 안드로이드 프로그래밍 6판
p.397 직접 풀어보기 10-1
[실습 10-1]을 다음과 같이 수정하라.
- ThirdActivity를 추가한다.
- 라디오 버튼으로 선택된 액티비티가 나오게 한다.
코드
MainActivity.java
package com.cookandroid.test;
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;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티 (수정)");
final RadioButton rdoSecond = findViewById(R.id.rdoSecond);
Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent;
if (rdoSecond.isChecked() == true)
intent = new Intent(getApplicationContext(), SecondActivity.class);
else
intent = new Intent(getApplicationContext(), ThirdActivity.class);
startActivity(intent);
}
});
}
}
activity_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rdoSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Second 액티비티" />
<RadioButton
android:id="@+id/rdoThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third 액티비티" />
</RadioGroup>
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기" />
</LinearLayout>
SecondActivity.java
package com.cookandroid.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("Second 액티비티");
Button btnReturn = findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:orientation="vertical" >
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기" />
</LinearLayout>
ThirdActivity.java
package com.cookandroid.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
setTitle("Third 액티비티");
Button btnReturn = findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:orientation="vertical" >
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기" />
</LinearLayout>
형광펜 칠한 부분을 넣어주어야 오류가 나지 않는다.
결과
728x90
'개발 > 책' 카테고리의 다른 글
[안드로이드 프로그래밍] 10장 직접 풀어보기 10-2 (0) | 2021.11.20 |
---|---|
[명품 웹 프로그래밍] 8장 Open Challenge (0) | 2021.11.15 |
[안드로이드 프로그래밍] 9장 직접 풀어보기 9-2 (0) | 2021.11.11 |