我想构建一个简单的 android 应用程序来计算三角形的面积。我有两个 textView 小部件、一个 editText 和一个按钮。当按下按钮时,它会将半径值传递给计算它的函数。我添加了一个变量并将其初始化为 5.0 只是为了测试目的,但是我想将放在 editText 字段中的值称为半径。
这是我的代码。包 com.circle;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class MainActivity extends Activity
{
private Button button1;
public EditText editText1;
public TextView textView1, textView2;
double radius = 5.0, area, perimeter;
public double calculateRadius(double radi)
{
area = Math.PI * (radius * radius);
return area;
}
public double calculatePerimeter(double rad)
{
perimeter = 2.0 * Math.PI * radius;
return perimeter;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.button1 = (Button) findViewById(R.id.button1);
this.editText1 = (EditText) findViewById(R.id.editText1);
this.textView1 = (TextView) findViewById(R.id.textView1);
this.textView1 = (TextView) findViewById(R.id.textView2);
this.button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View target)
{
calculateRadius(radius); //what's wrong?
calculatePerimeter(radius); //what's wrong??
textView1.setText("" + area); //what's wrong
textView2.setText("" + perimeter); //what's wrong?
}
});
}
}