Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

개발 공부

3. 다중 액티비티 활용 앱 본문

언어 공부/android

3. 다중 액티비티 활용 앱

방구석개발입문 2021. 6. 3. 17:31

하나의 앱은 보통 여러 화면으로 구성이 되는데 이러한 경우 사용자는 뷰를 클릭하고 스마트폰은 이에 반응해 다른 화면을 출력합니다.

android:clickable  = "true";           // 뷰를 클릭했을 때 반응 여부(true/false)
android:onClick = "callback" ;        // 뷰가 클릭되었을 때 자동으로 실행되는 메소드 이름

>>특정 이벤트가 발생했을 때 시스템이 인지하고 이벤트를 처리하기 위해 호출하는 메소드를 콜백(callback)메소드라고 합니다. 여기서 callback() 메소드가 이에 해당합니다.
이 메소드는 MainActivity에서 구현한 callback(View v)를 호출합니다.

액티비티의 호출액티비티간의 데이터 전달인텐트 클래스를 활용합니다.

 

activity_main.xml
<TextView
android:id="@+id/test_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Hello World!"
android:textSize="24sp"
android:textColor="#00ff00"
android:tag="1"
android:layout_gravity="center"
android:onClick="callback"
/>
mainActivity.java
public class MainActivity extends AppCompatActivity {

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

==================================================================
    public void callback(View v){              //TextView 클릭시 실행되는 메소드                                                      
        int id = v.getId();                         //터치한 뷰의 id 인식                                                                       
        LinearLayout layout = (LinearLayout) v.findViewById(id);         //터치한 뷰의 id에 해당하는 LinearLayout 인식     
        String tag = (String) layout.getTag();                                  //터치한 뷰의 tag값 추출                                
                                                                                                                                                         
        Toast.makeText(this, "클릭한 아이템 : " + tag, Toast.LENGTH_LONG ).show();   //Toast에 tag값 추출                  
                                                                                                                                                         
        Intent itent = new Intent(this, second.class);                                                                                        
        intent.putExtra("it_tag" , tag);                                                                                                            
        startActivity(intent);                                                                                                                        

=========================================================================

}
}
second.xml
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Hello World!"
android:textSize="24sp"
android:textColor="#00ff00"
android:tag="1"
android:layout_gravity="center"
android:onClick="ColseSecond"
/>
Second.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }
    
    TextView tv_label = (TextView) findViewById(R.id.label);

    Intent intent = getIntent();
    String tag = intent.getStringExtra("it_tag");

    Resources res = getResources();

    int id_label = res.getIdentifier ("label" + tag, "string", getPackageName());
    String label = res.getString(id_label);

    tv_label.setText(title);


    public void Close(View v){              //TextView 클릭시 실행되는 메소드                                                                                                                                                                    
        finish();
}
}

 

 

 

클래스 속성과 메소드 정리

VIew android : clickable 클릭 이벤트 반응 여부
android : id 뷰를 구별하기 위한 이름 
android:id ="@+id/아이디 이름"으로 지정
android : onClick 뷰 클릭시 실행되는 메소드 이름
android : tag 문자열로 나타내는 태그 
Toast static Toast makeText
(      ,      ,  )
makeText(this, "문자열", 출력시간)을 지정시 현재화면에 문자열이 시간만큼 나타나고 사라집니다.
시간은 미리 정의된 LENGTH_LONG과 LENGTH_SHORT사용가능
void show() 지정 시간동안 뷰를 보여줌
View final View findViewById(int id) id값에 해당하는 View를 인식
int getId() 뷰의 id값을 반환
Object getTag() tag값 반환
Activity void startAvtivity(intnet) 인텐트에서 지정한 액티비티를 실행
Intent Intent(String action, Uri uri) 주어진 uri에 대해 지정 액션하는 인텐트 생성
Intent putExtra
(String name, String value)
인텐트에 name값을 value로 할당

 

'언어 공부 > android' 카테고리의 다른 글

안드로이드 컴포넌트(구성요소)  (0) 2021.06.03
Intent (인텐트)  (0) 2021.06.03
2. 텍스트 출력  (0) 2021.06.03
1. 앱 프로젝트 구조  (0) 2021.06.02