본문 바로가기
App/Android Studio

Android Studio - 이벤트 리스너 활용 문제

by 코젼 2022. 8. 4.
728x90
반응형

2022-08-04(32일차)


📃버튼 클릭 시 토스트 메시지 출력


📝quiz1.xml

quiz1.xml
0.00MB

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button test"
        android:id="@+id/button"/>

</RelativeLayout>

📝MainActivity.java

package com.example.day33;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz1);
        button = findViewById(R.id.button);

        button.setOnClickListener(lis);
    }

    View.OnClickListener lis = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "버튼이 클릭됐습니다", Toast.LENGTH_SHORT).show();
        }
    };
}

📃버튼 클릭 이벤트

1. 첫 번 째 버튼 클릭 시 텍스트뷰의 텍스트 변경 “버튼이 눌렸습니다
2. 두 번 째 버튼 클릭 시 텍스트뷰 숨기기 다시 누르면 보이기


📝quiz2.xml

quiz2.xml
0.00MB

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="change" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_visible"
        android:text="숨기기/보이기" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:background="#ff8400"
        android:gravity="center"
        android:id="@+id/tv"
        android:text="텍스트뷰"/>


</LinearLayout>

📝MainActivity.java

package com.example.test02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn_change;
    Button btn_visible;
    TextView tv;

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

        btn_change = findViewById(R.id.btn);
        btn_visible = findViewById(R.id.btn_visible);
        tv = findViewById(R.id.tv);

        btn_change.setOnClickListener(lis);
        btn_visible.setOnClickListener(lis);
    }

    View.OnClickListener lis = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();

            if(id == btn_change.getId()) {
                Toast.makeText(MainActivity.this, "버튼이 눌렸습니다", Toast.LENGTH_SHORT).show();
            }
            else if(id == btn_visible.getId()) {
                if(tv.getVisibility() == view.VISIBLE) {
                    tv.setVisibility(view.INVISIBLE);
                }
                else {
                    tv.setVisibility(view.VISIBLE);
                }
            }
        }
    };
}

📃버튼 클릭 이벤트 (2)

1. 에디트텍스트에 작성한 글자가 ok 버튼 누르면 아래 텍스트뷰에 추가
2. cancel 버튼 누르면 에디트 텍스트 삭제


📝quiz3.xml

quiz3.xml
0.00MB

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름"
        android:id="@+id/tv_name"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_name"
        android:layout_alignBaseline="@id/tv_name"
        android:id="@+id/et_input"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CANCEL"
        android:layout_alignParentRight="true"
        android:layout_below="@id/et_input"
        android:id="@+id/btn_cancel"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:layout_below="@id/et_input"
        android:layout_toLeftOf="@id/btn_cancel"
        android:id="@+id/btn_ok"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btn_cancel"
        android:background="#c7f9c4"
        android:id="@+id/tv_add"/>

</RelativeLayout>

📝MainActivity.java

package com.example.test03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv_add;
    EditText et_input;
    Button btn_ok;
    Button btn_cancel;

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

        tv_add = findViewById(R.id.tv_add);
        et_input = findViewById(R.id.et_input);

        btn_ok = findViewById(R.id.btn_ok);
        btn_cancel = findViewById(R.id.btn_cancel);

        btn_ok.setOnClickListener(lis);
        btn_cancel.setOnClickListener(lis);
    }

    View.OnClickListener lis = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();

            if(id == btn_ok.getId()) {
                tv_add.append(et_input.getText().toString() + "\n");
                et_input.setText("");
            }
            else if(id == btn_cancel.getId()) {
                tv_add.setText("");
            }
        }
    };
}

📃터치 이벤트


📝quiz4.xml

quiz4.xml
0.00MB

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:padding="50dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="O"
            android:textSize="100dp"
            android:background="#FAB4B4"
            android:gravity="center"
            android:layout_marginRight="10dp"
            android:id="@+id/tv_1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="X"
            android:textSize="100dp"
            android:background="#52E8E8"
            android:gravity="center"
            android:layout_marginLeft="10dp"
            android:id="@+id/tv_2"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFEB3B"
        android:layout_margin="30dp"
        android:textSize="30dp"
        android:gravity="center"
        android:id="@+id/tv_result"
        />

</LinearLayout>

📝MainActivity.java

package com.example.test04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.tv_1).setOnTouchListener(lis);
        findViewById(R.id.tv_2).setOnTouchListener(lis);
    }

    View.OnTouchListener lis = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            String str = ((TextView)v).getText().toString();
            ((TextView)findViewById(R.id.tv_result)).setText(str+"가 눌렸습니다");
            return false;
        }
    };
}

 

728x90
반응형

댓글