Android Studio Java: 如何将EditText输入的值存储为double?

huangapple go评论84阅读模式
英文:

Android Studio Java: How to I store the value of an EditText Input as double?

问题

package com.example.durchschnittrechnen;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    EditText et_zahl1, et_zahl2, et_zahl3, et_zahl4, et_zahl5;
    TextView tv_ergebnis;
    Button btn_berechnen, btn_reset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupUI();
        reset();
        calculateAverage();
    }

    public void setupUI() {
        et_zahl1 = findViewById(R.id.et_zahl1);
        et_zahl2 = findViewById(R.id.et_zahl2);
        et_zahl3 = findViewById(R.id.et_zahl3);
        et_zahl4 = findViewById(R.id.et_zahl4);
        et_zahl5 = findViewById(R.id.et_zahl5);
        btn_berechnen = findViewById(R.id.btn_berechnen);
        btn_reset = findViewById(R.id.btn_reset);
        tv_ergebnis = findViewById(R.id.tv_ergebnis);
    }

    public void reset() {
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_zahl1.setText("");
                et_zahl2.setText("");
                et_zahl3.setText("");
                et_zahl4.setText("");
                et_zahl5.setText("");
                tv_ergebnis.setText("");
            }
        });
    }

    public void calculateAverage() {
        btn_berechnen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double input1 = Double.parseDouble(et_zahl1.getText().toString());
                double input2 = Double.parseDouble(et_zahl2.getText().toString());
                double input3 = Double.parseDouble(et_zahl3.getText().toString());
                double input4 = Double.parseDouble(et_zahl4.getText().toString());
                double input5 = Double.parseDouble(et_zahl5.getText().toString());

                double average = (input1 + input2 + input3 + input4 + input5) / 5;
                tv_ergebnis.setText(String.valueOf(average));
            }
        });
    }

}

Please note that the provided translation contains only the translated code portion without any additional content.

英文:

so I am working on a small application which reads 5 different inputs (double type) from 5 different EditTexts.
So each EditText field represents 1 input containing a double value.

A buttom named "calculate" then calculates the average by using (input1 + input2 + input3 + input4 + input5 / 5) and stores it into a TextView called "average".

My question is how do I access the input to use it in my calculation method?

This is what my code looks like so far:

package com.example.durchschnittrechnen;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
EditText et_zahl1, et_zahl2, et_zahl3, et_zahl4, et_zahl5;
TextView tv_ergebnis;
Button btn_berechnen, btn_reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
reset();
calculateAverage();
}
public void setupUI() {
et_zahl1 = findViewById(R.id.et_zahl1);
et_zahl2 = findViewById(R.id.et_zahl2);
et_zahl3 = findViewById(R.id.et_zahl3);
et_zahl4 = findViewById(R.id.et_zahl4);
et_zahl5 = findViewById(R.id.et_zahl5);
btn_berechnen = findViewById(R.id.btn_berechnen);
btn_reset = findViewById(R.id.btn_reset);
tv_ergebnis = findViewById(R.id.tv_ergebnis);
}
public void reset() {
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_zahl1.setText("");
et_zahl2.setText("");
et_zahl3.setText("");
et_zahl4.setText("");
et_zahl5.setText("");
tv_ergebnis.setText("");
}
});
}
public void calculateAverage() {
btn_berechnen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}

I have no idea how I proceed now.
Any help is welcome.
Thanks.

答案1

得分: 2

有一个getText()函数。它提供了文本框中的任何文本。然后您需要将其转换为Double。

String str = mEdit.getText().toString();
double dnum = Double.parseDouble(str);
英文:

There is a getText() function. It provides whatever text is in the box. You then would have to convert it to a Double.

 String str = mEdit.getText().toString();
double dnum = Double.parseDouble(str);

答案2

得分: 0

编辑文本值返回字符串,您可以将字符串转换为双精度数。这里是一个示例:

double et_zahl1_dbl = new Double(et_zahl1.getText().toString());

我建议在用户输入除双精度数以外的内容时添加用户消息提示。

英文:

Edit Text value returns string, you can covert string to double. Here is an example:

double et_zahl1_dbl = new Double(et_zahl1.getText().toString());

I do recommend adding user messaging, if user enters anything else besides double.

huangapple
  • 本文由 发表于 2020年10月6日 08:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64217740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定