ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [2] Life Cycle, Event
    Develpment/Android Sample Source 2020. 9. 13. 21:38

    1. Life Cycle


     Sameple Source 

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package com.ssh.user.ex_1201;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
     
    public class LifeActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_life);
     
            /* 로그
               안드로이드는 예외의 위치를 확인하거나 경로확인 등을 위해서
               로그를 사용한다.
             */
     
            Log.i("MY""--onCreate");
        }
     
        /* 호출순서(life cycle)
            시작시 : onCreate  -> onStart  ->  onResume
            홈버튼 : onPause   -> onStop (백그라운드)
            재실행 : onRestart -> onStart  ->  onResume (백그라운드에서 재실행)
            종료시 : onPause   -> onStop   ->  onDestroy
            onCreate    : 처음 실행 했을 경우 한번만 호출.
            onDestroy   : 실제 종료시에 한번만 호출.
            onRestart   : 정지 상태에서 실행했을 경우 호출.
            onStart -> onResume : 실행시 기본
            onPause -> onStop   : 종료시 기본(백그라운드)
         */
     
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.i("MY""--onDestroy");
        }
     
        @Override
        protected void onPause() {
            super.onPause();
            Log.i("MY""--onPause");
        }
     
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.i("MY""--onRestart");
        }
     
        @Override
        protected void onResume() {
            super.onResume();
            Log.i("MY""--onResume");
        }
     
        @Override
        protected void onStart() {
            super.onStart();
            Log.i("MY""--onStart");
        }
     
        @Override
        protected void onStop() {
            super.onStop();
            Log.i("MY""--onStop");
        }
    }
     
    cs



    2. Event


    Sample Source

     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    package com.ssh.user.ex_1201;
     
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class EventActivity extends AppCompatActivity {
     
        Button red, blue, toast;
        TextView txt;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_event);
     
            // Button 검색
            red     = (Button)findViewById(R.id.btn_red);
            blue    = (Button)findViewById(R.id.btn_blue);
            toast   = (Button)findViewById(R.id.btn_toast);
     
            // TextView 검색
            txt     = (TextView)findViewById(R.id.txt);
     
            // Button에 Event 감지자를 등록
            red.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Button 클릭시 호출되는 메서드
                    // TextView 배경색, 내용 변경
     
                    txt.setBackgroundColor( Color.RED );
                    txt.setText("RED");
                }
            });
     
            blue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    txt.setBackgroundColor( Color.BLUE );
                    txt.setText("BLUE");
                }
            });
     
            toast.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    // Context 정보에 EventActivity.this 로 넘겨 주어도 되지만, getApplicationContext()로 넘길경우 필요한 정보만 추려서 넘길수 있다.
                    // 보통 EventActivity.this를 사용. Toast의 경우에는 getApplicationContext() 이 좋다.
     
    //                Toast.makeText( EventActivity.this, "출력할 문자열 !!", Toast.LENGTH_LONG ).show();
                    Toast.makeText( getApplicationContext(), "출력할 문자열 !!", Toast.LENGTH_LONG ).show();
                }
            });
        }
    }
     
    cs


     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <?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:id="@+id/activity_event"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.ssh.user.ex_1201.EventActivity"
        android:orientation="vertical">
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
     
            <Button
                android:id="@+id/btn_red"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="red"
                android:textSize="20dp" />
     
            <Button
                android:id="@+id/btn_blue"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="blue"
                android:textSize="20dp"/>
     
            <Button
                android:id="@+id/btn_toast"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="toast"
                android:textSize="20dp" />
     
     
        </LinearLayout>
     
        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000"
            android:text="test"
            android:textSize="20dp"
            android:textColor="#fff"
            android:gravity="center" />
     
    </LinearLayout>
     
    cs


     



    3. Event2


     Sample Source

     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package com.ssh.user.ex_1201;
     
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.TextView;
     
    public class Event2Activity extends AppCompatActivity {
     
        Button btn;
        EditText edtxt;
        TextView txt;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_event2);
     
            btn     = (Button)findViewById(R.id.start);
            edtxt   = (EditText)findViewById(R.id.ed_txt);
            txt     = (TextView)findViewById(R.id.txt);
     
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String msg = edtxt.getText().toString();
                    int i;
                    for(i = 0; i < msg.length()/2; i++)
                        if(msg.charAt(i) != msg.charAt(msg.length()-1-i))
                            break;
     
                    if(i == msg.length()/2)
                        txt.setText("TRUE");
                    else
                        txt.setText("FALSE");
     
                }
            });
        }
    }
     
    cs


     

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <?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:id="@+id/activity_event2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.ssh.user.ex_1201.Event2Activity"
        android:orientation="vertical">
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <EditText
                android:id="@+id/ed_txt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:inputType="number"
                android:ems="10"
                android:hint="숫자를 입력하세요!!"/>
     
            <Button
                android:id="@+id/start"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Start"/>
     
        </LinearLayout>
     
        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#aaa"
            android:gravity="center"
            android:text="Result"
            android:textSize="30dp" />
     
    </LinearLayout>
     
    cs


     



    댓글

Designed by Tistory.