2swan

Android 화면 전환 본문

Programming/Android Example

Android 화면 전환

2swan 2023. 8. 3. 13:26

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 : 파란색 화면