ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [3] TouchEvent, Inflater
    Develpment/Android Sample Source 2020. 9. 13. 21:38

    1. Touch 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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package com.ssh.user.ex_1202;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
     
    public class TouchEventActivity extends AppCompatActivity {
     
        Button evt_btn;
        TextView txt_view, box;
        boolean isCheck;    // default = false
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_touch_event);
     
            evt_btn     = (Button)findViewById(R.id.evt_btn);
            txt_view    = (TextView)findViewById(R.id.txt_view);
            box         = (TextView)findViewById(R.id.box);
     
            evt_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    if(isCheck)
                        box.setText("NO EVENT!");
                    else
                        box.setText("EVENT");
     
                    isCheck = !isCheck;
                }
            });
     
            // box 텍스트 뷰에 터치 이벤트 감지자자
           box.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    String result = "";
                    int x = 0;
                    int y = 0;
     
                    switch(motionEvent.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:   // 터치(손가락이 닿았을때), return 값이 true, false에 상관없이 감지한다.
                            result = "down";
                            break;
     
                        case MotionEvent.ACTION_UP:     // return 값이 true일 경우에만 감지한다
                            result = "up";
                            break;
     
                        case MotionEvent.ACTION_MOVE:   // return 값이 true일 경우에만 감지한다.
                            x = (int)motionEvent.getX();
                            y = (int)motionEvent.getY();
                            result = "x : " + x + ", y : " + y;
                            break;
     
                    }//switch
     
                    txt_view.setText(result);
                    // return 값은 true일 경우에만 화면에 적용 하지만,
                    // Action_Down은 return 값에 관계없이 적용된다.
                    return isCheck;
                }
            });
     
     
        }
    }
     
    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
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_touch_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_1202.TouchEventActivity">
     
        <TextView
            android:id="@+id/txt_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp" />
     
        <!--버튼으로 이벤트 감지 on/off-->
        <Button
            android:id="@+id/evt_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="catch event"
            android:textSize="25dp"/>
     
     
        <!--빨간색 뷰만 터치를 감지 하도록 만듬-->
        <TextView
            android:id="@+id/box"
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:background="#f00"
            android:textColor="#fff"
            android:textSize="20dp"
            android:gravity="center"
            android:layout_centerInParent="true"/>
     
    </RelativeLayout>
     
    cs


     




    2. Inflater


     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
    package com.ssh.user.ex_1202;
     
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
     
    public class InflaterActivity extends AppCompatActivity {
     
        LayoutInflater linf;
        View sub;
        RelativeLayout parent;
        Button btn;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_inflater);
     
            parent = (RelativeLayout)findViewById(R.id.activity_inflater);
     
            // LayoutInflater는 .xml을 객체화(view) 시켜주는 클래스
            linf = (LayoutInflater)getSystemService( LAYOUT_INFLATER_SERVICE );
     
            //sub = linf.inflate(R.layout.sub_layout, parent);
     
            sub = linf.inflate(R.layout.sub_layout, null);
     
            parent.addView(sub);
     
     
            btn = (Button)sub.findViewById(R.id.btn_event);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), "event!!", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
     
    cs

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_inflater"
        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_1202.InflaterActivity">
     
    </RelativeLayout>
     
    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">
     
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
     
            <TextView
                android:id="@+id/list_form_txt_s"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="txt"/>
     
            <TextView
                android:id="@+id/list_form_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="7dp"
                android:text="line2"
                android:textSize="20dp"
                android:textStyle="bold"/>
     
        </LinearLayout>
     
    </LinearLayout>
    cs

     



    댓글

Designed by Tistory.