Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.er.latihan1;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.example.er.latihan1.R;
- public class MainActivity extends AppCompatActivity
- implements View.OnClickListener {
- private EditText edtWidth, edtHeight, edtLength;
- private Button btnCalculate;
- private TextView tvResult;
- private static String STATE_HASIL = "state_hasil";
- private String str;
- @Override
- protected void onCreate(Bundle saveInstanceState){
- super.onCreate(saveInstanceState);
- setContentView(R.layout.activity_main);
- edtWidth =(EditText)findViewById(R.id.edt_width);
- edtHeight = (EditText)findViewById(R.id.edt_height);
- edtLength = (EditText)findViewById(R.id.edt_length);
- btnCalculate = (Button)findViewById(R.id.btn_calculate);
- tvResult = (TextView)findViewById(R.id.tv_result);
- btnCalculate.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (v.getId()== R.id.btn_calculate) {
- String length = edtLength.getText().toString().trim();
- String width = edtWidth.getText().toString().trim();
- String height = edtHeight.getText().toString().trim();
- boolean isEmptyFields = false;
- boolean isInvalidDouble = false;
- if (TextUtils.isEmpty(length)) {
- isEmptyFields = true;
- edtLength.setError("Field ini tidak boleh kosong");
- }else if (!isDouble(length)) {
- isInvalidDouble = true;
- edtLength.setError("Field ini harus berupa nomor yang valid");
- }
- if (TextUtils.isEmpty(width)) {
- isEmptyFields = true;
- edtLength.setError("Field ini tidak boleh kosong");
- }else if (!isDouble(width)) {
- isInvalidDouble = true;
- edtLength.setError("Field ini harus berupa nomor yang valid");
- }
- if (TextUtils.isEmpty(height)) {
- isEmptyFields = true;
- edtLength.setError("Field ini tidak boleh kosong");
- }else if (!isDouble(height)) {
- isInvalidDouble = true;
- edtLength.setError("Field ini harus berupa nomor yang valid");
- }
- if (!isEmptyFields&&!isInvalidDouble) {
- double l = Double.parseDouble(length);
- double w = Double.parseDouble(width);
- double h = Double.parseDouble(height);
- double volume = l*w*h;
- tvResult.setText(String.valueOf(volume));
- }
- boolean isDouble(String str) {
- try {
- Double.parseDouble(str);
- return true;
- } catch (NumberFormatException e){
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement