안드로이드 수명 계산기 만들기 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">

 

    <ScrollView

        android:layout_width="match_parent"

        android:layout_height="match_parent">

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical"

            android:padding="10dp">

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Name" />

 

            <EditText

                android:id="@+id/et_name"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

            />

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Age" />

 

            <EditText

                android:id="@+id/et_age"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:digits="0123456789"

                android:inputType="number"/>

            <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp"

            android:text="Gender" />

 

            <RadioButton

                android:id="@+id/rb_male"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Male"/>

 

            <RadioButton

                android:id="@+id/rb_female"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Female"/>

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="20dp"

                android:text="Smoking" />

 

            <RadioButton

                android:id="@+id/rb_smoking_yes"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="YES"/>

 

            <RadioButton

                android:id="@+id/rb_smoking_no"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="NO"/>

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="20dp"

                android:text="Drugs" />

 

            <RadioButton

                android:id="@+id/rb_drugs_yes"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="YES"/>

 

            <RadioButton

                android:id="@+id/rb_drugs_no"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="NO"/>

 

            <Button

                android:id="@+id/b_calculator"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="10dp"

                android:text="CALCULATE"

                android:textSize="32sp"/>

 

            <TextView

                android:id="@+id/tv_result"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="10dp"

                android:text=""

                android:textSize="32sp"/>


        </LinearLayout>

    </ScrollView>


</LinearLayout>

 

 

액티비티 자바 코드 (안드로이드 라디오 버튼 다루는 방법을 터특한다.)

 

package com.jwlee.deathcalculator;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.TextView;

 

import java.util.Random;

 

public class MainActivity extends AppCompatActivity {

 

    EditText et_name, et_age;

    RadioButton rb_male, rb_female, rb_smoking_yes, rb_smoking_no, rb_drugs_yes, rb_drugs_no;

    Button b_calculate;

    TextView tv_result;

    Random r;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        et_name = findViewById(R.id.et_name);

        et_age = findViewById(R.id.et_age);

        rb_male = findViewById(R.id.rb_male);

        rb_female = findViewById(R.id.rb_female);

        rb_smoking_yes = findViewById(R.id.rb_smoking_yes);

        rb_smoking_no = findViewById(R.id.rb_smoking_no);

        rb_drugs_yes = findViewById(R.id.rb_drugs_yes);

        rb_drugs_no = findViewById(R.id.rb_drugs_no);

        b_calculate = findViewById(R.id.b_calculator);

        tv_result =findViewById(R.id.tv_result);

 

        r= new Random();

 

        //init stuff

        et_name.setText("Android");

        et_age.setText("30");

 

        rb_male.setChecked(true);

        rb_female.setChecked(false);

 

        rb_smoking_yes.setChecked(true);

        rb_smoking_no.setChecked(false);

 

        rb_drugs_yes.setChecked(true);

        rb_drugs_no.setChecked(false);

 

        tv_result.setText("");

 

        rb_male.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_male.setChecked(true);

                rb_female.setChecked(false);

 

            }

        });

 

        rb_female.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_male.setChecked(false);

                rb_female.setChecked(true);

 

            }

        });

 

        rb_smoking_yes.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_smoking_yes.setChecked(true);

                rb_smoking_no.setChecked(false);

 

            }

        });

 

        rb_smoking_no.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_smoking_yes.setChecked(false);

                rb_smoking_no.setChecked(true);

 

            }

        });

 

        rb_drugs_yes.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_drugs_yes.setChecked(true);

                rb_drugs_no.setChecked(false);

 

            }

        });

 

        rb_drugs_no.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_drugs_yes.setChecked(false);

                rb_drugs_no.setChecked(true);

 

            }

        });

 

        b_calculate.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(!et_name.getText().toString().equals("") && !et_age.getText().toString().equals("")) {

                    //get age and set it to 20 if lower

                    int currentAge = Integer.parseInt(et_age.getText().toString());

                    if(currentAge<20){

                        currentAge = 20;

                    }

 

                    //set random seed according to the name

                    int seed = et_name.getText().toString().hashCode();

                    r.setSeed(seed);

 

                    //calculate dying age

                    int ageOfDying = r.nextInt(120-currentAge) + currentAge;

 

                    //get the human stats

                    boolean isMale = rb_male.isChecked();

                    boolean isSmoking = rb_smoking_yes.isChecked();

                    boolean isTakingDrugs = rb_drugs_yes.isChecked();

 

                    //remove years for smoking, taking drugs or being a male

 

                    if(isMale){

                        ageOfDying = ageOfDying - 5;

 

                    }

 

                    if(isSmoking) {

                        ageOfDying = ageOfDying-5;

                    }

 

                    if(isTakingDrugs){

                        ageOfDying-=5;

 

                    }

                    if(ageOfDying <= currentAge)

                        ageOfDying = currentAge + 10;

 

                    tv_result.setText("" + ageOfDying);

                }

            }

        });

 

    }

}

 

 

1.  다음고 같은 권총 그림파일 세개를 만들어 리소트 drawable에 추가한다. 

 

2. 안드로이드 러시아 룰렛 게임 xml 코드 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

    android:padding="20dp"

    tools:context=".MainActivity">

 

    <ImageView

        android:id="@+id/imageView"

        android:scaleType="centerInside"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:src="@drawable/gun"

        android:layout_centerInParent="true"

        />

 

    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="ROLL"

        />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:gravity="center"

        android:text="Click on the image to shoot!"

        />

 

</RelativeLayout>

 

ㅁㅁㅁ

 

3. 액티비티 자바 코드 

 

package com.jwlee.russianroulettegame;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class MainActivity extends AppCompatActivity {

 

    ImageView imageView;

    Button button;

    TextView textView;

 

    List<String> bullets;

    boolean shuffled = true;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        imageView = findViewById(R.id.imageView);

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);



        bullets = new ArrayList<>();



        bullets.add("NO");

        bullets.add("YES");

 

        //roll the barrel

        Collections.shuffle(bullets);

 

        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                Collections.shuffle(bullets);

                imageView.setImageResource(R.drawable.gun);

                shuffled = true;

                textView.setText("Click on the image to shoot!");

 

            }

        });

 

        imageView.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(shuffled) {

                    String currentBullet = bullets.get(0);

                    if(currentBullet.equals("YES")){

                        imageView.setImageResource(R.drawable.bang);

                        textView.setText("You are daed!");

                    }else {

                        imageView.setImageResource(R.drawable.click);

                        textView.setText("You are alive! Pass the gun to the next player!");

                    }

                    shuffled = false;

                }else {

                    textView.setText("First Roll the barrel!");

                }




            }

        });

    }

}

 

4. 동영상보고 따라하기 

 

 

안드로이드 랜덤카드 발생하기, android random card generator

 

 

1. xml 코드 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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/button"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:text="GET A CARD"

       />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_above="@+id/button"

        android:layout_centerHorizontal="true"

 

        android:layout_marginBottom="40dp"

        android:text=""

        android:textSize="30sp"

        />



</RelativeLayout>

 

 

2. 액티비티 자바 코드 

package com.jwlee.randomcardgenerator;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

 

public class MainActivity extends AppCompatActivity {

 

    Button button;

    TextView textView;

 

    List<String> cardValues, cardsColors;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);

 

        cardValues = new ArrayList<>();

        cardsColors = new ArrayList<>();

 

        cardValues.add("2");

        cardValues.add("3");

        cardValues.add("4");

        cardValues.add("5");

        cardValues.add("6");

        cardValues.add("7");

        cardValues.add("8");

        cardValues.add("9");

        cardValues.add("10");

        cardValues.add("J");

        cardValues.add("Q");

        cardValues.add("K");

        cardValues.add("A");

 

        cardsColors.add("clubs");

        cardsColors.add("diamonds");

        cardsColors.add("heart");

        cardsColors.add("spades");




        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

 

                Random r = new Random();

 

                String randomValue = cardValues.get(r.nextInt(cardValues.size()));

                String randomColor = cardsColors.get(r.nextInt(cardsColors.size()));

 

                textView.setText(randomValue + " of " + randomColor);

 

            }

        });

 

    }

}

 

안드로이드 패스트 탭핑게임 Android Fast Tapping Game

 

1. 리소스 drawable에 다음과 같은 그림 파일을 삽입한다. 이미지 이름은 tag.png 

저 빨간 대쉬 네모칸을 빠르게 탭핑(손가락으로 빠르게 계속 터치)하는 것이다. 

 

2. XML 코딩한다. 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

    android:padding="10dp"

    tools:context=".MainActivity">

 

    <TextView

        android:id="@+id/tv_result"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="Start Tapping"

        />

 

    <TextView

        android:id="@+id/tv_info"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_centerHorizontal="true"

        android:text="Start Tapping"

        />

 

    <ImageView

        android:id="@+id/iv_tap"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:layout_centerInParent="true"

        android:src="@drawable/tap"

        />

 

</RelativeLayout>

 

 

ㅁㅁㅁ

 

3. 자바 액티비티 코딩한다. 

package com.jwlee.fasttappinggame;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.content.SharedPreferences;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.os.Handler;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

 

public class MainActivity extends AppCompatActivity {

 

    ImageView iv_tap;

    TextView tv_result, tv_info;

 

    int currentTaps = 0;

    boolean gameStarted = false;

 

    CountDownTimer timer;

 

    int bestResult = 0;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        iv_tap = findViewById(R.id.iv_tap);

        tv_result = findViewById(R.id.tv_result);

        tv_info = findViewById(R.id.tv_info);

 

        final SharedPreferences preferences = getSharedPreferences("PREES",0);

        bestResult = preferences.getInt("High Score",0);

 

        tv_result.setText("Best Result: "+bestResult);

 

        iv_tap.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(gameStarted){

                    currentTaps++;

                }else {

                    tv_info.setText("Tap, tap, tap !!!");

                    gameStarted = true;

                    timer.start();

                }

            }

        });

 

        //timer for 10 sec with interval 1 sec

        timer = new CountDownTimer(10000,1000) {

            @Override

            public void onTick(long millisUntilFinished) {

                long timeTillEnd = (millisUntilFinished / 1000) +1;

                tv_result.setText("Time Remaining: "+timeTillEnd);

 

            }

 

            @Override

            public void onFinish() {

                iv_tap.setEnabled(false);

                gameStarted = false;

                tv_info.setText("Game Over!");

 

                if(currentTaps > bestResult) {

                    bestResult = currentTaps;

 

                    SharedPreferences preferences1 = getSharedPreferences("PREES",0);

                    SharedPreferences.Editor editor = preferences1.edit();

                    editor.putInt("High Score", bestResult);

                    editor.apply();

                }

 

                tv_result.setText("Best Result: "+bestResult + "\nCurrent Taps: "+currentTaps);

 

                new Handler().postDelayed(new Runnable() {

                    @Override

                    public void run() {

                        iv_tap.setEnabled(true);

                        tv_info.setText("Start Tapping");

                        currentTaps = 0;

                    }

                },2000);

 

            }

        };

    }

}

 

 

+ Recent posts