ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] Intent 화면전환
    Android 2022. 3. 9. 00:09

    오늘은 실무에서 자주 쓰이는 중요한 코드다.

    좀 헷갈리긴 한다.

     

    대략적인 흐름은 이렇다.

    EditText에 string 입력 후 Move 버튼 누르면 putExtra()로 입력된 string 데이터를 받아서 Intent로 넘어간다.

    SubActivity에 오면 getIntent()로 데이터를 받는다. 이 때 보낼 때와 동일하게 string으로 받는다(getStringExtra()).

    받아 저장한 데이터를 setText()로 출력!

     

     

    [화면 구성]

    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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <EditText
            android:id="@+id/et_test"
            android:layout_width="200dp"
            android:layout_height="wrap_content"/>
    
    
        <Button
            android:id="@+id/btn_move"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Move"/>
        <!--Move 클릭하면 Sub로 이동하도록-->
    
    
    </LinearLayout>

    activity_sub.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=".SubActivity">
    
        <TextView
            android:id="@+id/tv_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textColor="#FFFFFF"/>
    
    </LinearLayout>

     

     

    [Java 코드]

    MainActivity.java

    package com.example.intent;
    
    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; //Button 클래스 생성 및 변수 선언
        private EditText et_test;
        private String str;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) { //onCreate : MainActivity 실행될 때 안에 있는 구문 실행해라
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et_test = findViewById(R.id.et_test); // et_test를 xml의 EditText와 연결
            btn_move = findViewById(R.id.btn_move); // main.xml 내 btn_move 와 연결
    
            btn_move.setOnClickListener(new View.OnClickListener() { // btn_move 클릭시 안쪽 구문 실행해라
                @Override
                public void onClick(View view) {
                    str = et_test.getText().toString(); //입력된 text를 받아 string으로 변환하여 str에 저장
                    Intent intent = new Intent(MainActivity.this, SubActivity.class); // intent 객체 생성
                    // MainActivity > SubActivity 방향 지정
                    intent.putExtra("str", str); // str에 데이터 받아서 Sub로 보냄
                    startActivity(intent); // Activity 이동
                }
            });
        }
    }

     

    SubActivity.java

    package com.example.intent;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    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); //TextView 와 id 연결
    
            Intent intent = getIntent(); // intent 로 오는 데이터 있으면 받겠다
            String str = intent.getStringExtra("str");//쏘는 곳과 이름 동일하게
            // string 입력했기 때문에 string으로 받아줌
    
            tv_sub.setText(str); // 위에서 받은 str 데이터 put
        }
    }

     

    'Android' 카테고리의 다른 글

    [Android] 패키지구조&역할  (0) 2022.03.09
    [Android] ImageView, Toast  (0) 2022.03.09
    [Android]EditText  (0) 2022.03.08
    [Android]TextView#2  (0) 2022.03.08
    [Android]TextView #1  (0) 2022.03.08
Designed by Tistory.