ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • MainActivity.java (poemApp01)
    학교/Android 2024. 4. 17. 20:02

    * 메인 액티비티를 정의하고 있다. 

    앱의 초기화면을 구성한다. 

     

     

    package com.example.b_poemapp01;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void ShowPoem(View view){
    
            int id = view.getId();
            LinearLayout layout = (LinearLayout) findViewById(id);
            String tag = (String) layout.getTag();
    
            Intent it = new Intent(this, ShowPoem.class);
            it.putExtra("it_tag", tag);
            startActivity(it);
        }
    
        public void ShowTrip(View view){
            int id = view.getId();
            ImageView imageView = (ImageView) findViewById(id);
            String tag = (String) imageView.getTag();
    
            Intent it = new Intent(this, ShowTrip.class);
            it.putExtra("it_tag", tag);
            startActivity(it);
        }
    }

     

     

     

     

    1. public class MainActivity extends AppCompatActivity { ... } : MainActivity 클래스를 정의한다.

    AppCompatActivity 클래스를 확장하여 기본적인 액티비티 동작을 상속받는다. 

    AppCompatActivity 는 안드로이드 호환성을 제공하는 클래스로, 최신 Android SDK와 호환되도록 해준다.

     

    2. onCreate(Bundle savedInstanceState) : 액티비티가 생성될 때 호출되는 생명주기 메서드이다.

    여기서는 액티비티의  초기화를 담당한다.

     

    3. super.onCreate(savedInstanceState) : 상위 클래스의 onCreate() 메서드를 호출하여 기본 초기화 작업을 수행한다.

     

    4. setContentView(R.layout.activity_main) : 액티비티에 표시될 레이아웃을 설정한다. 

    여기서는 activity_main.xml 레이아웃 파일을 사용하여 액티비티의 화면을 구성한다.

     

    5. public void ShowPoem(View view) : 사용자가 시를 선택했을 때 호출되는 콜백 메서드이다. 

     

    6. int id = view.getId() : 사용자가 클릭한 뷰의 ID를 가져온다.

     

    7. LinearLayout layout = (LinearLayout) findViewById(id) : 해당 ID에 해당하는 LinearLayout 뷰를 찾는다.

    이 뷰에서 tag 속성 값을 가져올 것이다.

     

    8. String tag = (String) layout.getTag() : LinearLayout의 tag 속성 값을 가져온다. 이 값은 사용자가 선택한 시의 정보를 나타낸다. 

     

    9. Intent it = new Intent(this, ShowPoem.class) : 새로운 액티비티로 이동하기 위한 Intent 객체를 생성한다.

    이때, 현재 액티비티(MainiActivity) 에서 ShowPoem 액티비티로 이동할 것임을 명시한다. 

     

    10. it.putExtra("it_tag", tag) : Intent에 사용자가 선택한 시의 정보를 추가한다. 

    이 정보는 it_tag 라는 이름으로 전달된다. 

     

    11. startActivity(it) : 생성된 Intent를 사용하여 새로운 액티비티를 시작한다.

     

    12. public void ShowTrip(View view) : 사용자가 여행을 선택했을 때 호출되는 콜백 메서드이다. 

    위의 ShowPoem() 메서드와 동일한 구조를 가지고 있지만, 여행을 보여주는 액티비티로 이동하는데 사용된다. 

Designed by Tistory.