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