头一次搞学习搞的这个晚。。。
貌似好久都没来这个论坛了,可能是放暑假的原因吧,离开下越来越近了,意味着校园时光逐渐离我们远去。。
运用面向对象的思想+单例模式以下是主要代码- package com.feng;
- import android.app.Activity;
- import android.widget.Button;
- import android.widget.EditText;
- public class Buttons extends Activity {
- private Buttons() {}
- private static Buttons instance;
- EditText et_play; // 显示
- Button bt_0;
- Button bt_1;
- Button bt_2;
- Button bt_3;
- Button bt_4;
- Button bt_5;
- Button bt_6;
- Button bt_7;
- Button bt_8;
- Button bt_9;
- Button bt_dot;
- Button bt_back;
- Button bt_add;
- Button bt_sub;
- Button bt_multiply;
- Button bt_div;
- Button clean;
- Button bt_equal;
-
- public static Buttons getInstance() {
- if(instance == null)
- instance = new Buttons();
- return instance;
- }
- }
复制代码- package com.feng;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- final static Buttons bt = Buttons.getInstance();
- static StringBuffer str_display = new StringBuffer();
- static int judge_point = 0; //判断是否打了小数点 0表示没打小数点 1表示打了一个 2表示不能在打
- static double num1 = 0.0;
- static double num2 = 0.0;
- static char sign; //判断按了什么计算符合
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.lay_layout);
- bt.et_play = (EditText) findViewById(R.id.et);;
- bt.et_play.setText("0.0");
-
- bt.bt_0 = (Button) findViewById(R.id.bt_0);
- bt.bt_1 = (Button) findViewById(R.id.bt_1);
- bt.bt_2 = (Button) findViewById(R.id.bt_2);
- bt.bt_3 = (Button) findViewById(R.id.bt_3);
- bt.bt_4 = (Button) findViewById(R.id.bt_4);
- bt.bt_5 = (Button) findViewById(R.id.bt_5);
- bt.bt_6 = (Button) findViewById(R.id.bt_6);
- bt.bt_7 = (Button) findViewById(R.id.bt_7);
- bt.bt_8 = (Button) findViewById(R.id.bt_8);
- bt.bt_9 = (Button) findViewById(R.id.bt_9);
- bt.bt_dot = (Button) findViewById(R.id.bt_point);
- bt.bt_back = (Button) findViewById(R.id.bt_back);
- bt.clean = (Button)findViewById(R.id.bt_clear);
- bt.bt_add = (Button)findViewById(R.id.bt_add);
- bt.bt_sub = (Button)findViewById(R.id.bt_sub);
- bt.bt_multiply = (Button)findViewById(R.id.bt_multiply);
- bt.bt_div = (Button)findViewById(R.id.bt_divide);
- bt.bt_equal = (Button)findViewById(R.id.bt_equal);
-
- onClickListener(bt.bt_0, "0");
- onClickListener(bt.bt_1, "1");
- onClickListener(bt.bt_2, "2");
- onClickListener(bt.bt_3, "3");
- onClickListener(bt.bt_4, "4");
- onClickListener(bt.bt_5, "5");
- onClickListener(bt.bt_6, "6");
- onClickListener(bt.bt_7, "7");
- onClickListener(bt.bt_8, "8");
- onClickListener(bt.bt_9, "9");
- onClickListener(bt.bt_dot, ".");
- onClickListener(bt.clean);
- onClickListener(bt.bt_add, '+');
- onClickListener(bt.bt_sub, '-');
- onClickListener(bt.bt_multiply, '*');
- onClickListener(bt.bt_div, '/');
-
- bt.bt_back.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if(!(str_display.length() == 0)) {
- str_display.deleteCharAt(str_display.length() - 1);
- bt.et_play.setText(str_display);
- }
- }
-
- });
- bt.bt_equal.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(!(str_display.length() == 0)) {
-
- num1 = Double.valueOf(str_display.toString());
- System.out.println(sign);
- switch(sign) {
- case '+':
- bt.et_play.setText(String.valueOf((num1 + num2))); break;
- case '-':
- bt.et_play.setText(String.valueOf((num2 - num1))); break;
- case '*':
- bt.et_play.setText(String.valueOf((num2 * num1))); break;
- case '/':
- bt.et_play.setText(String.valueOf((num2 / num1))); break;
-
- }
-
- str_display = new StringBuffer();
- judge_point = 0;
- }
- }
-
- });
- }
-
- @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;
- }
- public static void onClickListener(Button button, final String num) {
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(str_display.indexOf(".") == 0) {
- str_display.insert(0, "0");
- bt.et_play.setText(str_display.toString());
- }
- if(num .equals(".") && judge_point == 0) {
- judge_point++;
- str_display.append(num);
- bt.et_play.setText(str_display.toString());
- }
- else if(num.equals(".")) {
- return;
- }
- else {
- str_display.append(num);
- bt.et_play.setText(str_display.toString());
- }
- }
-
- });
- }
- public static void onClickListener(Button button) {
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- str_display = new StringBuffer();
- judge_point = 0;
- bt.et_play.setText("0.0");
- num1 = 0.0;
- num2 = 0.0;
- }
-
- });
- }
- public static void onClickListener(Button button, final char symbol) {
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(!(str_display.length() == 0))
- {
- num1 = Double.valueOf(str_display.toString());
- num2 = num1;
- str_display = new StringBuffer();
- judge_point = 0;
- bt.et_play.setText(String.valueOf(symbol));
- //num2 = Double.valueOf(str_display.toString());
- }
- sign = symbol;
- }
-
- });
- }
- }
复制代码 OK 睡觉了···早上自己用手机 测设·····里面只是简单的两个数+ - / * 。
|
|