Android 计算圆的面积
计应111 陈佳
创建一个android项目,这里我取名chen
打开该项目下res-->latout-->activity_main.xml(xml是android的界面文件)
拖入一个EditText和button
修改textview的id为txttishi,
text为请输入该圆的半径
修改editText的id为etxt
修改button的Text为计算
然后对button注册绑定事件
b1=(Button) findViewById(R.id.button1);
etxt=(EditText) findViewById(R.id.etxt);
txttishi=(TextView) findViewById(R.id.txttishi);
b1.setOnClickListener(this);
再对button的点击事件进行处理
try{
if(java.lang.Character.isDigit(etxt.getText().toString().charAt(0))){
float banjin=Integer.parseInt(etxt.getText().toString());
etxt.setText("");
float mianji=(float) (banjin*banjin*Math.PI);
BigDecimal b = new BigDecimal(mianji);
float f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();
etxt.setText("该圆的面积是:"+String.valueOf(f1));
}
else{
new AlertDialog.Builder(this)
.setTitle("错误")
.setMessage("请输入数字")
.setPositiveButton("OK", null)
.show();
}
}
catch (Exception e) {
new AlertDialog.Builder(this)
.setTitle("错误")
.setMessage("输入错误请核对")
.setPositiveButton("OK", null)
.show();
}
运行如下:
输入一个数如:123
结果如下:
就编程来说不是最难的,最难的是搞懂了数字之间的传值.还有就是每一步点击发生的情况。