클릭횟수 보여주는 앱 만들기

ino5·2022년 6월 12일
0

처음 만든 앱이에요.

이미지 클릭하면 옆에 숫자 증가함

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_clicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        />

    <ImageView
        android:id="@+id/img_clicker"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>

MainActivity.java

package com.example.inoapplicationclicker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView img_clicker;
    Button btn_clicker;
    int cnt = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_clicker = (Button) findViewById(R.id.btn_clicker);
        img_clicker = (ImageView) findViewById(R.id.img_clicker);
        img_clicker.setOnClickListener((v) -> {
            cnt++;
            btn_clicker.setText(Integer.toString(cnt));
        });
    }
}
profile
지금은 네이버 블로그만 해요... https://blog.naver.com/chero77

0개의 댓글