所以,我正在创建一个应用程序,其主要目的是计算三角形的面积,周长和内径。用户应该输入三个double类型的值。 &安培;他们点击的任何按钮都应该在结果视图中吐出答案。用户可以单击任何按钮(无论哪个都没关系)。如果他们先点击“区域”按钮&然后决定点击“周长”按钮,结果应该改变。这是一张图片,让您了解它的样子:(http://tinypic.com/r/v3ncyh/8)。我不确定我是否正确编程,因为我的值看起来完全不正确&我不认为我的视图与我的代码正确连接。这段代码的第一部分是我的Triangle类,其中包含了我的所有getter / setter&我计算面积,周长和内径的方法。第二部分是我的Activity,它是我的应用程序的主要(和唯一)活动。
public class Triangle {
// instance variables
private double sideA;
private double sideB;
private double sideC;
// Getters and setters
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSide2B() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getSideC() {
return sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
// Constructor
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
// Three methods...
public double getPerimeter() {
return sideA + sideB + sideC;
}
public double getArea() {
double s = 1/2 * (sideA + sideB + sideC);
double area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
return area;
}
public double getInRadius() {
double r = ((-sideA + sideB + sideC)*(sideA - sideB + sideC)*(sideA + sideB - sideC))/(4*(sideA + sideB + sideC));
double answer = Math.sqrt(r);
return r;
}
}
public class TriangleComputationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triangle_computation);
// Declare our View variables and assign them the Views from the layout file
final TextView lengthOne = (TextView) findViewById(R.id.lengthOne);
final TextView lengthTwo = (TextView) findViewById(R.id.lengthTwo);
final TextView lengthThree = (TextView) findViewById(R.id.lengthThree);
final TextView result = (TextView) findViewById(R.id.result);
Button areaButton = (Button) findViewById(R.id.area);
Button perimeterButton = (Button) findViewById(R.id.perimeter);
Button inradiusButton = (Button) findViewById(R.id.inradius);
final Triangle triangle = new Triangle((double)lengthOne.getInputType(), (double)lengthTwo.getInputType(),(double)lengthThree.getInputType());
// Methods in Action...
areaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText(triangle.getArea());
}
});
perimeterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText(triangle.getPerimeter());
}
});
inradiusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setText(triangle.getInRadius());
}
});
}
答案 0 :(得分:1)
TextView.getInputType()
不会返回对您有用的值。它与TextView的实际文本无关。
您应该使用TextView.getText()
代替。您必须使用Double.parseInt()
或Double.valueOf()
将其转换为双精度数字。如果是我,我会在那些TextViews上设置android:inputType="numberDecimal"
。