IT/Android

android - Activity, Intent 응용 (액티비티 화면 세개 전환)

노마드오브 2018. 9. 22. 22:12

파일명 : activity_main.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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"/>

<Button
android:id="@+id/btnFisish1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="닫기"
android:onClick="close1" />
</LinearLayout>


파일명 : 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="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:orientation="vertical">

<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"
android:onClick="onBtn" />
<Button
android:id="@+id/btnFisish2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="닫기"
android:onClick="close1" />

</LinearLayout>


파일명 : 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:orientation="vertical"
tools:context=".ThirdActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second 액티비티" />
<RadioButton
android:id="@+id/rButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메인 액티비티" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="새화면열기"/>
<Button
android:id="@+id/btnFisish3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="닫기"
android:onClick="close1" />
</LinearLayout>
</LinearLayout>


파일명 : MainActivity.java

package com.example.it.myapplication8_2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
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);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}

void close1 (View view) {
finish();
}
}


파일명 : SecondActivity.java

package com.example.it.myapplication8_2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("두번째");
}

void onBtn (View view) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}

void close1 (View view) {
finish();
}

}


파일명 : ThirdActivity.java

package com.example.it.myapplication8_2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class ThirdActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
setTitle("세번째");

Button btn1 = (Button) findViewById(R.id.btn1);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton selectedRdo = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
// String selectedValue = selectedRdo.getText().toString(); // 라디오 버튼 값

Intent intent;
String str = "";

switch (rg.getCheckedRadioButtonId()) {
case R.id.rButton1 :
intent = new Intent(ThirdActivity.this, SecondActivity.class);
startActivity(intent);
str = "a";
break;
case R.id.rButton2 :
intent = new Intent(ThirdActivity.this, MainActivity.class);
startActivity(intent);
str = "b";
break;
}

}
}
);
}

void close1 (View view) {
finish();
}

}


파일명 : AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it.myapplication8_2">

<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/AppTheme">
<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"></activity>
<activity android:name=".ThirdActivity"></activity>
</application>

</manifest>