본문 바로가기
Android

안드로이드 Intent 화면전환

by Son 2022. 1. 6.

<?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">

   <EditText    //input text
       android:id="@+id/et_test"
       android:layout_width="200dp"
       android:layout_height="wrap_content"/>

   <Button  //button
       android:id="@+id/btn_move"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="이동"/>

</LinearLayout>
---------------------------------------------------------------------------------------------------------------

<?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=".SubActivity">

    <TextView  //text보기
        android:id="@+id/tv_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="서브 액티비티 도착"/>

</LinearLayout>

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.example.intentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button btn_move;
    private EditText et_test;
    private String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_test = findViewById(R.id.et_test);





        btn_move = findViewById(R.id.btn_move);  //버튼 정보를 btn_move에 넣기
        btn_move.setOnClickListener(new View.OnClickListener() {  //button이 클릭되었을때
            @Override
            public void onClick(View view) {
                str = et_test.getText().toString();  //우리가 입력한 텍스트를 toString으로 바꾸고 저장
                Intent intent = new Intent(MainActivity.this , SubActivity.class); //화면 전환을 저장
                intent.putExtra("str", str);  //intent에 우리가 입력한 텍스트 str을 저장 
                startActivity(intent); // 액티비티 이동
            }
        });

    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?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=".SubActivity">

    <TextView  //text보여주기
        android:id="@+id/tv_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="서브 액티비티 도착"/>

</LinearLayout>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.example.intentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class SubActivity extends AppCompatActivity {

    private TextView tv_sub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        tv_sub = findViewById(R.id.tv_sub);

        Intent intent = getIntent();
       String str = intent.getStringExtra( "str"); //우리가 입력한 input text를 불러와서 str에 저장


        tv_sub.setText(str);  // tv_sub에 우리가 입력한 text를 보여주도록 돌려줌


    }
}



'Android' 카테고리의 다른 글

안드로이드 ListView  (0) 2022.01.13
안드로이드 패키지 구조  (0) 2022.01.13
안드로이드 ImageView Toast  (0) 2022.01.12
안드로이드 예제1  (0) 2022.01.05
안드로이드 텍스트뷰 글자크기 클자색상 레이아웃  (0) 2022.01.03