2swan
Android 화면 전환 본문
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:id="@+id/rdoGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rdoSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second 액티비티"
android:checked="true"/>
<RadioButton
android:id="@+id/rdoThird"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Third 액티비티"/>
<RadioButton
android:id="@+id/rdoFour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Four 액티비티"/>
</RadioGroup>
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"/>
</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=".SecondActivity"
android:orientation="vertical"
android:background="#00ff00">
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
<Button
android:id="@+id/btnNewbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새화면"/>
</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=".ThirdActivity"
android:orientation="vertical"
android:background="#ff0000">
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기" />
</LinearLayout>
four.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=".FourActivity"
android:orientation="vertical"
android:background="#0000ff">
<Button
android:id="@+id/btnReturn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기" />
</LinearLayout>
main.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioButton rdoSecond = findViewById(R.id.rdoSecond);
RadioButton rdoThird = findViewById(R.id.rdoThird);
RadioButton rdoFour = findViewById(R.id.rdoFour);
Button button = findViewById(R.id.btnNewActivity);
rdoFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FourActivity.class); //main -> four
startActivity(intent);
}
});
button.setOnClickListener(new View.OnClickListener() {
Intent intent = null;
@Override
public void onClick(View view) {
if(rdoSecond.isChecked()){
intent = new Intent(getApplicationContext(), SecondActivity.class); //main -> second
} else if (rdoThird.isChecked()) {
intent = new Intent(getApplicationContext(), ThirdActivity.class); //main -> third
}
startActivity(intent);
}
});
second.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btnReturn = findViewById(R.id.btnReturn);
Button btnNButton = findViewById(R.id.btnNewbtn);
btnReturn.setOnClickListener(new View.OnClickListener() { //돌아가기
@Override
public void onClick(View view) {
finish();
}
});
btnNButton.setOnClickListener(new View.OnClickListener() { //새 화면
@Override
public void onClick(View view) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); //second화면 -> third 화면
startActivity(intent);
}
});
}
}
third.java
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
Button btnReturn = findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //돌아가기
finish();
}
});
}
}
four.java
public class FourActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
Button btnReturn4 = findViewById(R.id.btnReturn4);
btnReturn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
결과 값
- Second : 초록색 화면
- Third : 빨간색 화면
- Four : 파란색 화면
'Programming > Android Example' 카테고리의 다른 글
Android 액티비티 데이터 전달 예제 launcher (더하기) (0) | 2023.08.04 |
---|---|
Android 액티비티 데이터 전달 예제 (0) | 2023.08.04 |
Android Rating Bar 증가 (0) | 2023.08.03 |
Android 투표 앱 예제 (0) | 2023.08.03 |
Android 날짜/시간 설정 (0) | 2023.08.03 |