package com.example.Calcuator; import com.wk.cal.CalAdd; import com.wk.cal.CalDiv; import com.wk.cal.CalMul; import com.wk.cal.CalSub; import com.wk.cal.Calculator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MyActivity extends Activity { TextView mtvText; Button mbtnAdd, mbtnSub, mbtnMul, mbtnDiv, mbtnResult, mbtnClear, mbtn0, mbtn1, mbtn2, mbtn3, mbtn4, mbtn5, mbtn6, mbtn7, mbtn8, mbtn9,mbtnExit; String num1, num2; String cal_Operator = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initButton(); AddButtonAction(); } private void initButton() { mbtnClear = (Button) findViewById(R.id.btnClear); mtvText = (TextView) findViewById(R.id.tvResult); mbtnAdd = (Button) findViewById(R.id.btnAdd); mbtnSub = (Button) findViewById(R.id.btnSub); mbtnMul = (Button) findViewById(R.id.btnMul); mbtnDiv = (Button) findViewById(R.id.btnDiv); mbtnResult = (Button) findViewById(R.id.btnResult); mbtnExit = (Button) findViewById(R.id.btnExit); mbtn0 = (Button) findViewById(R.id.btn0); mbtn1 = (Button) findViewById(R.id.btn1); mbtn2 = (Button) findViewById(R.id.btn2); mbtn3 = (Button) findViewById(R.id.btn3); mbtn4 = (Button) findViewById(R.id.btn4); mbtn5 = (Button) findViewById(R.id.btn5); mbtn6 = (Button) findViewById(R.id.btn6); mbtn7 = (Button) findViewById(R.id.btn7); mbtn8 = (Button) findViewById(R.id.btn8); mbtn9 = (Button) findViewById(R.id.btn9); } private void AddButtonAction() { mbtn0.setOnClickListener(new ButtonActionListener()); mbtn1.setOnClickListener(new ButtonActionListener()); mbtn2.setOnClickListener(new ButtonActionListener()); mbtn3.setOnClickListener(new ButtonActionListener()); mbtn4.setOnClickListener(new ButtonActionListener()); mbtn5.setOnClickListener(new ButtonActionListener()); mbtn6.setOnClickListener(new ButtonActionListener()); mbtn7.setOnClickListener(new ButtonActionListener()); mbtn8.setOnClickListener(new ButtonActionListener()); mbtn9.setOnClickListener(new ButtonActionListener()); mbtnAdd.setOnClickListener(new ButtonActionListener()); mbtnSub.setOnClickListener(new ButtonActionListener()); mbtnMul.setOnClickListener(new ButtonActionListener()); mbtnDiv.setOnClickListener(new ButtonActionListener()); mbtnResult.setOnClickListener(new ButtonActionListener()); mbtnClear.setOnClickListener(new ButtonActionListener()); mbtnExit.setOnClickListener(new ButtonActionListener()); } class ButtonActionListener implements OnClickListener { public void btnClearAction(View v) { if (v.getId() == R.id.btnClear) { mtvText.setText(""); } } public void btnAddAction(View v) { if (v.getId() == R.id.btnAdd) { num1 = (String) mtvText.getText(); System.out.println("[num1]"+num1); mtvText.setText(""); cal_Operator = (String) mbtnAdd.getText(); } } public void btnSubAction(View v) { if (v.getId() == R.id.btnSub) { num1 = (String) mtvText.getText(); System.out.println("[num1]" + num1); mtvText.setText(""); cal_Operator = (String) mbtnSub.getText(); } } public void btnMulAction(View v) { if (v.getId() == R.id.btnMul) { num1 = (String) mtvText.getText(); System.out.println("[num1]"+num1); mtvText.setText(""); cal_Operator = (String) mbtnMul.getText(); } } public void btnDivAction(View v) { if (v.getId() == R.id.btnDiv) { num1 = (String) mtvText.getText(); System.out.println("[num1]"+num1); System.out.println(num1); mtvText.setText(""); cal_Operator = (String) mbtnDiv.getText(); } } public void btn0Action(View v) { if (v.getId() == R.id.btn0) { mtvText.setText(mtvText.getText() + "0"); } } public void btn1Action(View v) { if (v.getId() == R.id.btn1) { mtvText.setText(mtvText.getText() + "1"); } } public void btn2Action(View v) { if (v.getId() == R.id.btn2) { mtvText.setText(mtvText.getText() + "2"); } } public void btn3Action(View v) { if (v.getId() == R.id.btn3) { mtvText.setText(mtvText.getText() + "3"); } } public void btn4Action(View v) { if (v.getId() == R.id.btn4) { mtvText.setText(mtvText.getText() + "4"); } } public void btn5Action(View v) { if (v.getId() == R.id.btn5) { mtvText.setText(mtvText.getText() + "5"); } } public void btn6Action(View v) { if (v.getId() == R.id.btn6) { mtvText.setText(mtvText.getText() + "6"); } } public void btn7Action(View v) { if (v.getId() == R.id.btn7) { mtvText.setText(mtvText.getText() + "7"); } } public void btn8Action(View v) { if (v.getId() == R.id.btn8) { mtvText.setText(mtvText.getText() + "8"); } } public void btn9Action(View v) { if (v.getId() == R.id.btn9) { mtvText.setText(mtvText.getText() + "9"); } } public void btnResultAction(View v) { try{ if(v.getId() == R.id.btnResult){ num2 = (String) mtvText.getText(); System.out.println("[num2]"+num2); mtvText.setText(""); CalFactory factory = new CalFactory(); factory.createCalFactory(cal_Operator); } }catch(Exception e){ } } public void btnExitAction(View v){ if(v.getId() == R.id.btnExit){ System.exit(0); } } @Override public void onClick(View v) { btnExitAction(v); btnClearAction(v); btn0Action(v); btn1Action(v); btn2Action(v); btn3Action(v); btn4Action(v); btn5Action(v); btn6Action(v); btn7Action(v); btn8Action(v); btn9Action(v); btnAddAction(v); btnSubAction(v); btnMulAction(v); btnDivAction(v); btnResultAction(v); } } class CalFactory { public void createCalFactory(String cal_Operator) { Calculator cal = null; switch (cal_Operator) { case "+": cal = new CalAdd(); cal.setNum1(Double.valueOf(num1)); cal.setNum2(Double.valueOf(num2)); System.out.println("[num1]"+cal.getNum1()+"[num2]"+cal.getNum2()); mtvText.setText("加法"+num1+"[参数2]"+num2); mtvText.setText(String.valueOf(cal.getResult())); break; case "―": cal = new CalSub(); cal.setNum1(Double.valueOf(num1)); cal.setNum2(Double.valueOf(num2)); mtvText.setText(String.valueOf(cal.getResult())); break; case "×": cal = new CalMul(); cal.setNum1(Double.valueOf(num1)); cal.setNum2(Double.valueOf(num2)); mtvText.setText(String.valueOf(cal.getResult())); break; case "÷": cal = new CalDiv(); cal.setNum1(Double.valueOf(num1)); cal.setNum2(Double.valueOf(num2)); mtvText.setText(String.valueOf(cal.getResult())); break; } } } }
这个我只上传一个主要的Activity 界面的代码,核心的理念和我的控制台计算器理念是一模一样的..
时间: 2024-10-05 19:28:04