我正在做一个android应用程序 - 当我设法让我运行一个问题出现时,我有很多错误。我必须得到一个编辑文本的价值并将它变成一个双倍的数据,起初它根本没有工作(应用程序因为它而崩溃),然后我设法让它运行,但现在它始终为零获取一个编辑文本值到一个双 - 总是得到零(android studio,java)
对于示例每当方法C2F称为resukt是32 ...
**Main activity:**
input = (EditText) findViewById(R.id.input);
convert = (Button) findViewById(R.id.convert);
result = (TextView) findViewById(R.id.result);
c2f = (RadioButton) findViewById(R.id.c2f);
c2k = (RadioButton) findViewById(R.id.c2k);
f2c = (RadioButton) findViewById(R.id.f2c);
f2k = (RadioButton) findViewById(R.id.f2k);
k2c = (RadioButton) findViewById(R.id.k2c);
k2f = (RadioButton) findViewById(R.id.k2f);
double w;
try {
w = new Double(input.getText().toString());
} catch (NumberFormatException e) {
w = 0;
}
final double finalW = w;
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (c2f.isChecked())
{
result.setText(Converter.c2f(finalW)+ "F");
} else if (c2k.isChecked())
{
result.setText(Converter.c2k(finalW)+ "K");
} else if (f2c.isChecked())
{
result.setText(Converter.f2c(finalW)+ "C");
} else if (f2k.isChecked())
{
result.setText(Converter.f2k(finalW)+ "K");
} else if (k2c.isChecked())
{
result.setText(Converter.k2c(finalW)+ "C");
} else if (k2f.isChecked())
{
result.setText(Converter.k2f(finalW)+ "F");
}
}
});
}}
类转换
public class Converter
{ 公共静态双C2F(双瓦特){返回瓦特* 9/5 + 32;} public static double c2k(double w) { return w + 273.15; } public static double f2c(double w) { return(w-32)* 5/9; } public static double f2k(double w){return(w + 459.67)* 5/9;} public static double k2c(double w) { return w-273.15; } public static double k2f(double w) { return w * 1.8 - 459.67; } }
你好,谢谢你的回答。我有它在我的XML指定。没有得到我要说的地方:w = Double.parseDouble(input.getText()。toString()。trim()); –
@MarusiaPetrova,检查编辑的答案。 –