赞
踩
其中activity_main.xml文件为计算器的界面,shape_screen.xml为计算器显示器的图形,button1_presssed.xml为按键按压下的图形,button1_nomal.xml为按键未被按压下的图形,button1_shape.xml是为了凸显按键按下和弹起时呈现不同效果的文件。其余的文件类似。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#000000" tools:context="com.example.calculator.MainActivity"> <TextView //计算器显示屏 android:id="@+id/calculator_screen" android:layout_width="match_parent" android:layout_height="170dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:background="@drawable/shape_screen" android:gravity="right|bottom" android:hint="0" android:paddingRight="5dp" android:textColor="#000000" android:textSize="50sp"/> //各按键 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="vertical" android:gravity="center_horizontal|center_vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="%" android:background="@drawable/button1_shape" android:textSize="40sp" android:id="@+id/button_percent" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="÷" android:background="@drawable/button1_shape" android:textSize="45sp" android:id="@+id/button_divide" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button1_shape" android:text="×" android:textSize="45sp" android:id="@+id/button_multiply" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="C" android:background="@drawable/button2_shape" android:textSize="40sp" android:id="@+id/button_clear" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:layout_marginBottom="5dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="7" android:textSize="40sp" android:background="@drawable/button3_shape" android:id="@+id/button_7" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="8" android:background="@drawable/button3_shape" android:textSize="40sp" android:id="@+id/button_8" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="9" android:background="@drawable/button3_shape" android:textSize="40sp" android:id="@+id/button_9" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="+" android:background="@drawable/button2_shape" android:textSize="40sp" android:id="@+id/button_add" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="4" android:textSize="40sp" android:background="@drawable/button3_shape" android:id="@+id/button_4" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="5" android:background="@drawable/button3_shape" android:textSize="40sp" android:id="@+id/button_5" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="6" android:background="@drawable/button3_shape" android:textSize="40sp" android:id="@+id/button_6" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:text="-" android:background="@drawable/button2_shape" android:textSize="50sp" android:id="@+id/button_minus" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button3_shape" android:text="1" android:textSize="40sp" android:id="@+id/button_1" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button3_shape" android:text="2" android:textSize="40sp" android:id="@+id/button_2" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button3_shape" android:text="3" android:textSize="40sp" android:id="@+id/button_3" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button2_shape" android:text="√" android:textSize="40sp" android:id="@+id/button_radic" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button3_shape" android:text="0" android:textSize="40sp" android:id="@+id/button_0" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="10dp"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button3_shape" android:text="." android:textSize="45sp" android:id="@+id/button_dot" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="10dp"/> <Button android:layout_width="160dp" android:layout_height="80dp" android:background="@drawable/button4_shape" android:text="=" android:textSize="45sp" android:id="@+id/button_equal" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="10dp"/> </LinearLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="#c0c0c0"/> <size android:width="30dp" android:height="30dp"> </size> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"> </padding> <stroke android:width="1dp" android:color="#000000">//描边 </stroke> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="#ffffff"/> <size android:width="30dp" android:height="30dp"> </size> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"> </padding> <stroke android:width="1dp" android:color="#000000">//描边 </stroke> </shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button1_pressed"/>
<item android:drawable="@drawable/button1_nomal"/>
</selector>
package com.example.calculator; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.app.Activity; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView calculator_monitor; private Button but_0,but_1,but_2,but_3,but_4,but_5,but_6,but_7,but_8,but_9,but_clear,but_divide, but_multiply,but_add,but_minus,but_radic,but_dot,but_equal,but_percent; double num1=0,num2=0; double result=0;//Calculation results int operation=0;//Operands @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calculator_monitor=(TextView)findViewById(R.id.calculator_screen); but_0=(Button)findViewById(R.id.button_0); but_1=(Button)findViewById(R.id.button_1); but_2=(Button)findViewById(R.id.button_2); but_3=(Button)findViewById(R.id.button_3); but_4=(Button)findViewById(R.id.button_4); but_5=(Button)findViewById(R.id.button_5); but_6=(Button)findViewById(R.id.button_6); but_7=(Button)findViewById(R.id.button_7); but_8=(Button)findViewById(R.id.button_8); but_9=(Button)findViewById(R.id.button_9); but_add=(Button)findViewById(R.id.button_add); but_clear=(Button)findViewById(R.id.button_clear); but_divide=(Button)findViewById(R.id.button_divide); but_dot=(Button)findViewById(R.id.button_dot); but_equal=(Button)findViewById(R.id.button_equal); but_minus=(Button)findViewById(R.id.button_minus); but_multiply=(Button)findViewById(R.id.button_multiply); but_radic=(Button)findViewById(R.id.button_radic); but_percent=(Button)findViewById(R.id.button_percent); but_0.setOnClickListener(new MyOnClickListener());//加监视器 but_1.setOnClickListener(new MyOnClickListener()); but_2.setOnClickListener(new MyOnClickListener()); but_3.setOnClickListener(new MyOnClickListener()); but_4.setOnClickListener(new MyOnClickListener()); but_5.setOnClickListener(new MyOnClickListener()); but_6.setOnClickListener(new MyOnClickListener()); but_7.setOnClickListener(new MyOnClickListener()); but_8.setOnClickListener(new MyOnClickListener()); but_9.setOnClickListener(new MyOnClickListener()); but_clear.setOnClickListener(new MyOnClickListener()); but_divide.setOnClickListener(new MyOnClickListener()); but_minus.setOnClickListener(new MyOnClickListener()); but_add.setOnClickListener(new MyOnClickListener()); but_equal.setOnClickListener(new MyOnClickListener()); but_multiply.setOnClickListener(new MyOnClickListener()); but_dot.setOnClickListener(new MyOnClickListener()); but_radic.setOnClickListener(new MyOnClickListener()); but_percent.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements View.OnClickListener{ public void onClick(View v){//点击事件的处理方法 switch (v.getId()){ case R.id.button_clear: calculator_monitor.setText(null); break; case R.id.button_0: String str0=calculator_monitor.getText().toString();//获取输入的内容并转换为string型 str0+="0"; calculator_monitor.setText(str0); break; case R.id.button_1: String str1=calculator_monitor.getText().toString(); str1+="1"; calculator_monitor.setText(str1); break; case R.id.button_2: String str2=calculator_monitor.getText().toString(); str2+="2"; calculator_monitor.setText(str2); break; case R.id.button_3: String str3=calculator_monitor.getText().toString(); str3+="3"; calculator_monitor.setText(str3); break; case R.id.button_4: String str4=calculator_monitor.getText().toString(); str4+="4"; calculator_monitor.setText(str4); break; case R.id.button_5: String str5=calculator_monitor.getText().toString(); str5+="5"; calculator_monitor.setText(str5); break; case R.id.button_6: String str6=calculator_monitor.getText().toString(); str6+="6"; calculator_monitor.setText(str6); break; case R.id.button_7: String str7=calculator_monitor.getText().toString(); str7+="7"; calculator_monitor.setText(str7); break; case R.id.button_8: String str8=calculator_monitor.getText().toString(); str8+="8"; calculator_monitor.setText(str8); break; case R.id.button_9: String str9=calculator_monitor.getText().toString(); str9+="9"; calculator_monitor.setText(str9); break; case R.id.button_dot: String strdot=calculator_monitor.getText().toString(); strdot+="."; calculator_monitor.setText(strdot); break; case R.id.button_add: String stradd=calculator_monitor.getText().toString(); if(stradd.equals(null)){ return; } num1=Double.valueOf(stradd); stradd+="+"; calculator_monitor.setText(null); operation=1; break; case R.id.button_minus: String strminus=calculator_monitor.getText().toString(); if(strminus.equals(null)){ return; } num1=Double.valueOf(strminus); strminus+="-"; calculator_monitor.setText(null); operation=2; break; case R.id.button_multiply: String strmultiply=calculator_monitor.getText().toString(); if(strmultiply.equals(null)){ return; } num1=Double.valueOf(strmultiply); strmultiply+="*"; calculator_monitor.setText(null); operation=3; break; case R.id.button_divide: String strdivide=calculator_monitor.getText().toString(); if(strdivide.equals(null)){ return; } num1=Double.valueOf(strdivide); strdivide+="/"; calculator_monitor.setText(null); operation=4; break; case R.id.button_percent: String strpercent=calculator_monitor.getText().toString(); if(strpercent.equals(null)){ return; } num1=Double.valueOf(strpercent); strpercent+="%"; operation=5; break; case R.id.button_equal: String strequ=calculator_monitor.getText().toString(); num2=Double.valueOf(strequ); // calculator_monitor.setText(null); switch (operation){ case 1: result=num1+num2; calculator_monitor.setText(String.valueOf(num1)+"+"+String.valueOf(num2)+"="+String.valueOf(result)); break; case 2: result=num1-num2; calculator_monitor.setText(String.valueOf(num1)+"-"+String.valueOf(num2)+"="+String.valueOf(result)); break; case 3: result=num1*num2; calculator_monitor.setText(String.valueOf(num1)+"×"+String.valueOf(num2)+"="+String.valueOf(result)); break; case 4: if(num2==0){ calculator_monitor.setText("被除数不能为0!"); } else { result = num1 / num2; calculator_monitor.setText(String.valueOf(num1) + "÷" + String.valueOf(num2) + "=" + String.valueOf(result)); } break; case 5: result=num1/100; calculator_monitor.setText(String.valueOf(num1) + "%" +"="+String.valueOf(result)); break; default: result=0; break; } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。