어릴적 사촌형들과 전자계산기로 1 + 1 ===== '=' 을 

1분동안 많이 누르기 게임이 생각나서 간단히 만들어 봤습니다.

아래 이미지는 겔노트2 에서 실행화면 캡쳐 한겁니다.


Screenshot_2013-11-21-20-51-47.png


이하 풀 소스...


package com.example.addnumber;


import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;


public class AddNumber extends Activity{

private Button startBtn;

private Button addBtn;

private TextView tvStatus;

private TextView tvTime;


@Override

protected void onCreate(Bundle savedInstanceState) {

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

super.onCreate(savedInstanceState);

LayoutParams lp;

lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

RelativeLayout gameView = new RelativeLayout(this);

gameView.setLayoutParams(lp);

setContentView(gameView);

LinearLayout cv = new LinearLayout(this);

cv.setLayoutParams(lp);

cv.setOrientation(LinearLayout.VERTICAL);

cv.setBackgroundColor(Color.rgb(255, 255, 255));

gameView.addView(cv);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

TextView tvLabel = new TextView(this);

tvLabel.setLayoutParams(lp);

tvLabel.setBackgroundColor(Color.BLACK);

tvLabel.setTextColor(Color.WHITE);

tvLabel.setHeight(200);

tvLabel.setTextSize(18f);

tvLabel.setGravity(Gravity.CENTER);

tvLabel.setText("30 초 동안 빠르게 많이 터치하는 게임입니다.");

cv.addView(tvLabel);

tvStatus = new TextView(this);

tvStatus.setLayoutParams(lp);

tvStatus.setTextSize(28f);

tvStatus.setGravity(Gravity.CENTER);

tvStatus.setText("0 회 터치 하셨습니다.");

cv.addView(tvStatus);

tvTime = new TextView(this);

tvTime.setLayoutParams(lp);

tvTime.setBackgroundColor(Color.BLACK);

tvTime.setTextColor(Color.WHITE);

tvTime.setHeight(200);

tvTime.setTextSize(42f);

tvTime.setGravity(Gravity.CENTER);

tvTime.setText("30 초");

cv.addView(tvTime);

startBtn = new Button(this);

startBtn.setLayoutParams(lp);

startBtn.setTextSize(32f);

startBtn.setText("START GAME");

startBtn.setOnClickListener(BtnClick);

cv.addView(startBtn);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

addBtn = new Button(this);

addBtn.setLayoutParams(lp);

addBtn.setHeight(300);

addBtn.setTextSize(32f);

addBtn.setText("TOUCH BUTTON");

addBtn.setOnClickListener(BtnClick);

cv.addView(addBtn);

}

private int TouchCount = 0;

private View.OnClickListener BtnClick = new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v == startBtn){

timeCount = 30;

TouchCount = 0;

tvTime.setText("30 초.");

checkTime();

}

else if(v == addBtn && timeCount > 0){

TouchCount ++;

tvStatus.setText(String.valueOf(TouchCount)+" 회 터치 하셨습니다.");

}

}

};

private int timeCount=30;

private Handler handler;

private void checkTime(){

if(handler==null) handler = new Handler();

        handler.postDelayed(new Runnable(){

        public void run(){

        if(--timeCount>0){

        tvTime.setText(String.valueOf(timeCount)+" 초.");

            checkTime();

        }else{

        tvTime.setText("30 초.");

        }

        }

        }, 1000);

}


}