finish the java code
This commit is contained in:
@ -2,12 +2,63 @@ package me.jj97181818.ch06_energycalculator;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
|
||||
double[] energyRate = {3.1, 4.4, 13.2, 9.7, 5.1, 3.7};
|
||||
EditText weight, time;
|
||||
TextView total, txvRate;
|
||||
Spinner sports;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
//設定初值
|
||||
weight = findViewById(R.id.weight);
|
||||
time = findViewById(R.id.timeSpan);
|
||||
total = findViewById(R.id.total);
|
||||
txvRate = findViewById(R.id.txvRate);
|
||||
sports = findViewById(R.id.sports);
|
||||
|
||||
//註冊監聽器
|
||||
sports.setOnItemSelectedListener(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
txvRate.setText(String.valueOf(energyRate[position]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
|
||||
}
|
||||
|
||||
public void calc(View v) {
|
||||
String w = weight.getText().toString();
|
||||
String t = time.getText().toString();
|
||||
if (w.isEmpty() || w.equals(".") || t.isEmpty() || t.equals(".")) {
|
||||
total.setText("請輸入體重及運動時間");
|
||||
return;
|
||||
}
|
||||
|
||||
int pos = sports.getSelectedItemPosition();
|
||||
|
||||
long kcal = Math.round(energyRate[pos] * Double.parseDouble(w) * Double.parseDouble(t));
|
||||
|
||||
total.setText(String.format("消耗能量 %d 仟卡", kcal));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user