Android课程---计算器的实现
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hanqi.test5"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LongClickActivityActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".UIActivity1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".CalculatorActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity1" /> </application> </manifest>
package com.hanqi.test5; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class CalculatorActivity extends Activity implements View.OnClickListener { EditText et; Button bt_clear; Button bt_goback; //全局变量 Button bt_7; Button bt_8; Button bt_9; Button bt_6; Button bt_5; Button bt_4; Button bt_3; Button bt_2; Button bt_1; Button bt_0; Button bt_jia; Button bt_jian; Button bt_cheng; Button bt_chu; Button bt_dian; Button bt_deng; //存储显式的内容 //StringBuilder:操作字符串的工具类 private StringBuilder str_show = new StringBuilder();//实例化 //当前运算符按钮的id private int id_yunsf = 0; //运算符前面的数值 Double:包装类 private Double number_1; //运算符后面的数值 private Double number_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator); et = (EditText)findViewById(R.id.et); bt_clear = (Button)findViewById(R.id.clear); bt_goback = (Button)findViewById(R.id.goback); bt_7 = (Button)findViewById(R.id.bt_7); bt_8 = (Button)findViewById(R.id.bt_8); bt_9 = (Button)findViewById(R.id.bt_9); bt_6 = (Button)findViewById(R.id.bt_6); bt_5 = (Button)findViewById(R.id.bt_5); bt_4 = (Button)findViewById(R.id.bt_4); bt_3 = (Button)findViewById(R.id.bt_3); bt_2 = (Button)findViewById(R.id.bt_2); bt_1 = (Button)findViewById(R.id.bt_1); bt_0 = (Button)findViewById(R.id.bt_0); bt_jia = (Button)findViewById(R.id.bt_jia); bt_jian = (Button)findViewById(R.id.bt_jian); bt_cheng = (Button)findViewById(R.id.bt_cheng); bt_chu = (Button)findViewById(R.id.bt_chu); bt_dian = (Button)findViewById(R.id.bt_dian); bt_deng = (Button)findViewById(R.id.bt_deng); bt_clear.setOnClickListener(this); bt_goback.setOnClickListener(this); bt_7.setOnClickListener(this); bt_8.setOnClickListener(this); bt_9.setOnClickListener(this); bt_6.setOnClickListener(this); bt_5.setOnClickListener(this); bt_4.setOnClickListener(this); bt_3.setOnClickListener(this); bt_2.setOnClickListener(this); bt_1.setOnClickListener(this); bt_0.setOnClickListener(this); bt_jia.setOnClickListener(this); bt_jian.setOnClickListener(this); bt_cheng.setOnClickListener(this); bt_chu.setOnClickListener(this); bt_dian.setOnClickListener(this); bt_deng.setOnClickListener(this); } //事件源组件 public void onClick(View v) { Button bt = (Button)v; int id = bt.getId(); switch (id) { case R.id.clear: str_show = new StringBuilder(); //重新实例化就可以清除 //设置显示框内容 et.setText(str_show); //et:显示框 number_1 = null; number_2 = null; id_yunsf = 0; break; case R.id.goback: if (str_show.length()>0) { //删除最后一个字符 索引值减1 str_show.deleteCharAt(str_show.length()-1); et.setText(str_show); break; } case R.id.bt_7: case R.id.bt_8: case R.id.bt_9: case R.id.bt_6: case R.id.bt_5: case R.id.bt_4: case R.id.bt_3: case R.id.bt_2: case R.id.bt_1: case R.id.bt_0: if (id == R.id.bt_0 ||(id == R.id.bt_0 && !str_show.toString().equals("0"))) { str_show.append(bt.getText()); } et.setText(str_show); break; case R.id.bt_deng: case R.id.bt_jia: case R.id.bt_jian: case R.id.bt_cheng: case R.id.bt_chu: if (number_1 == null) { if (str_show.length()>0) { //把字符串转成Double number_1 = new Double(str_show.toString()); id_yunsf = bt.getId(); str_show = new StringBuilder(); //重新实例化就可以清除 //设置显示框内容 //et.setText(str_show); //et:显示框 } } else//计算 { if (str_show.length()>0) { //把字符串转成Double 赋值 number_2 = new Double(str_show.toString()); } switch (id_yunsf) { case R.id.bt_jia: //运算 double res = number_1.doubleValue() + number_2.doubleValue(); break; case R.id.bt_jian: //运算 number_1 = number_1.doubleValue() - number_2.doubleValue(); break; case R.id.bt_cheng: //运算 number_1 = number_1.doubleValue() * number_2.doubleValue(); break; case R.id.bt_chu: //运算 if (number_2 !=0) { number_1 = number_1.doubleValue() / number_2.doubleValue(); } else { Toast.makeText(this,"不能除0",Toast.LENGTH_LONG).show(); } break; } //运算符 id_yunsf = bt.getId(); //显示当前运算结果 et.setText(number_1.toString()); //清空 str_show = new StringBuilder(); } break; } } }
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4"> <!--行:rowCount 列:columnCount--> <EditText android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:layout_rowWeight="1" android:textSize="30sp" android:editable="false" android:gravity="right|center_vertical"/> <!--水平填充:fill_horizontal 竖直填充:fill_vertical 行号:layout_row 列号:layout_column(行号和列号都可以指定,但注意都是从0开始的)--> <Button android:text="清除" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/clear"/> <Button android:text="后退" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:textColor="#0ff" android:id="@+id/goback"/> <Button android:text="/" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_chu"/> <Button android:text="x" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_cheng"/> <Button android:text="7" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_7"/> <Button android:text="8" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_8"/> <Button android:text="9" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_9"/> <Button android:text="-" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_jian"/> <Button android:text="4" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_4"/> <Button android:text="5" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_5"/> <Button android:text="6" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_6"/> <Button android:text="+" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_jia"/> <Button android:text="1" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_1"/> <Button android:text="2" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_2"/> <Button android:text="3" android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:id="@+id/bt_3"/> <Button android:text="=" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:id="@+id/bt_deng"/> <Button android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20sp" android:background="#FF0000" android:id="@+id/bt_0"/> <Button android:text="." android:layout_rowWeight="1" android:layout_columnWeight="1" android:textSize="20sp" android:textColor="#FF0000" android:id="@+id/bt_dian"/> </GridLayout>
这些代码可以实现计算器的基本功能
效果: