Android 求圆的面积
计算机应用112班 孔秋静
在Android中实现求圆的面积。
在Layout 中添加一个EditText控件并将它的ID设为e1,添加一个TextViw控件并设置它的ID为t1,添加一个Buttom控件并设置ID为b1,。用EditText控件来接收输入的半径r,用TextViw来显示圆求出来的面积,单击Buttom时实现求圆面积的操作。
代码如下:
package com.cqvie;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SdActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button) findViewById(R.id.b1);
t1=(TextView) findViewById(R.id.t1);
e1=(EditText) findViewById(R.id.e1);
b1.setOnClickListener(this);
}
Button b1;
TextView t1;
EditText e1;
public void onClick(View arg0) {
// TODO Auto-generated method stub
float r,a;
r=Float.parseFloat(e1.getText().toString());
a=(float) (3.14159*r*r);
DecimalFormat df=new DecimalFormat("0.00");
t1.setText(df.format(a));
}
}
效果如下:
posted on 2013-06-07 11:18 孔秋静(1131601213) 阅读(165) 评论(0) 编辑 收藏 举报