我创建了一个应用程序来计算圆的面积和周长,然后将结果发送给用户。唯一的问题是我必须将计算放在与 MainActivity.java 不同的类中。我目前在 MainActivity.java 的底部有计算,这不是我想要的。这是我到目前为止所拥有的:
package com.areacircumferencecircle;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private Button btn;
private EditText edit;
private TextView area;
private TextView crf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
edit = (EditText)findViewById(R.id.editText1);
area = (TextView)findViewById(R.id.textArea);
crf = (TextView)findViewById(R.id.textCircumference);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
if(btn == v){
double r = Double.parseDouble(edit.getText().toString());
String a = Double.toString(3.141592 * (r * r));
String c = Double.toString(2 * 3.141592 * r);
crf.setText("Circumference: " + c);
area.setText("Area: " + a);
}
}
}
很感谢任何形式的帮助!