计算器在日常生活中用处还是比较大的,而且将其置入手机中,用起来更方便。而本次的任务是要运用MVC模式实现一个简易计算器。
阶段1、进行计算器的界面布局--V,这一阶段相对来说没有难度。
activity_calculate.xml文件:
string.xml文件中对各个控件的具体显示内容进行设置
Calculate
Hello world!
Settings
CalculateActivity
运算符
=
+
-
*
/
关于
退出
作者:牛灵玉\n版本:2.0
确定
取消
阶段2、编写计算器的业务逻辑类--M,实现计算器的加、减、乘、除基本功能
package com.edu.nly.model;
public class Calculator {
public double add(double a,double b){
return a+b;
}
public double sub(double a,double b){
return a-b;
}
public double mul(double a,double b){
return a*b;
}
public double div(double a,double b){
return a/b;
}
}
阶段3、4、5、编写Activity---C,查找组件并进行事件处理
package com.edu.nly;
import java.text.DecimalFormat;
import com.edu.nly.model.Calculator;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class CalculateActivity extends Activity {
// 定义组件
private EditText number1;// 操作数1文本框
private EditText number2;// 操作数2文本框
private TextView operate;// 运算符组件
private TextView result;// 存放结果的组件
private Button addButton;// 加法按钮组件
private Button subButton;// 减法按钮组件
private Button mulButton;// 乘法按钮组件
private Button divButton;// 除法按钮组件
private final static int MENU_ABOUT=1;
private final static int MENU_EXIT=2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculate);
initialComponents();
registerListener();
}
/*
* 注册监听事件
*/
private void registerListener() {
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String num1String = number1.getText().toString().trim();
String num2String = number2.getText().toString().trim();
Log.e("第一个数:",num1String);
Log.d("第二个数:",num2String);
operate.setText("+");
result.setText(Double.valueOf(new Calculator().add(
Double.parseDouble(num1String),
Double.parseDouble(num2String))).toString() );
}
});
subButton.setOnClickListener(new ButtonHander(){
public void onClick(View v) {
String num1String = number1.getText().toString().trim();
String num2String = number2.getText().toString().trim();
Log.e("第一个数:",num1String);
Log.d("第二个数:",num2String);
operate.setText("-");
result.setText(Double.valueOf(calculator.sub(
Double.parseDouble(num1String),
Double.parseDouble(num2String))).toString());
}
});
mulButton.setOnClickListener(new ButtonHander(){
public void onClick(View v) {
String num1String = number1.getText().toString().trim();
String num2String = number2.getText().toString().trim();
Log.e("第一个数:",num1String);
Log.d("第二个数:",num2String);
operate.setText("*");
result.setText(format(calculator.mul(
Double.parseDouble(num1String),
Double.parseDouble(num2String))));
}
});
divButton.setOnClickListener(new ButtonHander(){
public void onClick(View v) {
String num1String = number1.getText().toString().trim();
String num2String = number2.getText().toString().trim();
Log.e("第一个数:",num1String);
Log.d("第二个数:",num2String);
if("0".equals(num2String.trim())){
Toast.makeText(CalculateActivity.this, "除数不能为零", Toast.LENGTH_LONG).show();
}
operate.setText("/");
result.setText(format(calculator.div(
Double.parseDouble(num1String),
Double.parseDouble(num2String))));
}
});
}
/*
* 自定义初始化组件
*/
private void initialComponents() {
number1 = (EditText) this.findViewById(R.id.num1);
number2 = (EditText) this.findViewById(R.id.num2);
operate = (TextView) this.findViewById(R.id.operate);
result = (TextView) this.findViewById(R.id.result);
addButton = (Button) this.findViewById(R.id.add);
subButton = (Button) this.findViewById(R.id.sub);
mulButton = (Button) this.findViewById(R.id.mul);
divButton = (Button) this.findViewById(R.id.div);
}
private class ButtonHander implements OnClickListener {
String num1String = number1.getText().toString().trim();
String num2String = number2.getText().toString().trim();
Calculator calculator = new Calculator();
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
operate.setText("+");
break;
case R.id.sub:
operate.setText("-");
break;
case R.id.mul:
operate.setText("*");
break;
case R.id.div:
if("0".equals(num2String.trim())){
Toast.makeText(CalculateActivity.this, "除数不能为零", Toast.LENGTH_LONG).show();
break;
}
operate.setText("/");
break;
}
}
}
public String format(double result) {
DecimalFormat decimalFormat = new DecimalFormat("0.00");
return decimalFormat.format(result);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId=item.getItemId();
switch(itemId){
case MENU_ABOUT:
AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
alertDialog.setIcon(android.R.drawable.ic_menu_help);//引用系统自带的图标
alertDialog.setTitle(R.string.about);//给对话框设置标题
alertDialog.setMessage(R.string.info);//设置对话框的提示信息
alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(CalculateActivity.this, "欢迎你的使用", Toast.LENGTH_LONG).show();
}
});
alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.create().show();
break;
case MENU_EXIT:
this.finish();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ABOUT, 1, R.string.about);
menu.add(0, MENU_EXIT, 2, R.string.exit);
return true;
}
}
经过上述操作之后的运行结果:
点击确定之后的运行结果: