TextView,EditText,Buttonを使った問題。
Q1
身長(cm)と体重(kg)を入力させBMIを出力するアプリを作成せよ。
BMIは
体重(kg)/(身長(m)*身長(m))
にて求めることができる。
詳しくは実行例を参照のこと
[実行例]
スタート画面

値を入力しCALCボタンを押すと結果が表示される

●activity_main.xml
<?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:background="@android:color/holo_blue_dark"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:text="身長を入力(cm)"
android:textColor="@android:color/white"
android:textSize="18sp" />
<EditText
android:id="@+id/etHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etHeight"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="体重を入力(kg)"
android:textColor="@android:color/white"
android:textSize="18sp" />
<EditText
android:id="@+id/etWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/etHeight"
android:layout_below="@+id/textView2"
android:layout_marginTop="13dp"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btCalc"
android:text="CALC!"
android:layout_below="@+id/etWeight"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="24sp" />
</RelativeLayout>
●MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText etHeight,etWeight;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etHeight=(EditText)findViewById(R.id.etHeight);
etWeight=(EditText)findViewById(R.id.etWeight);
tvResult=(TextView)findViewById(R.id.tvResult);
}
public void btCalc(View v){
double height=Double.parseDouble(etHeight.getText().toString())/100.0d;
double weight=Double.parseDouble(etWeight.getText().toString());
double bmi=weight/(height*height);
tvResult.setText(String.format("あなたのBMIは%.1fです。",bmi));
}
}
コメント