android studio实现计算器
本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下
效果图:
资源文件:
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="white">#FFFFFF</color> <color name="black">#000000</color> <color name="zi">#FFFFFF</color> <color name="gray">#BEBEBE</color> <color name="green">#9AFF9A</color> <color name="littlegreen">#F0FFFF</color> </resources>
white.xml
设置input text的填充色为白色
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="@color/white"/> </shape>
selector.xml
点击按钮时产生阴影效果
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/littlegreen" android:state_pressed="true"/> <item android:drawable="@color/white" /> </selector>
equeal.xml
同理,等号的阴影效果
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/white" android:state_pressed="true"/> <item android:drawable="@color/littlegreen" /> </selector>
布局文件
<?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="@drawable/jisuanqi" tools:context="com.example.administrator.calculate.MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="20dp"> <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="60dp" android:background="@drawable/white" android:editable="false" android:gravity="right|bottom" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="C" android:background="@drawable/selector" android:gravity="center" android:textSize="25sp" android:id="@+id/clear" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="←" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="23sp" android:id="@+id/delete" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="×" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/cheng" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="÷" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/clu" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="7" android:background="@drawable/selector" android:gravity="center" android:textSize="25sp" android:id="@+id/num7" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="8" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num8" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="9" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num9" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="-" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/charjian" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="4" android:background="@drawable/selector" android:gravity="center" android:textSize="25sp" android:id="@+id/num4" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="5" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num5" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="6" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num6" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="+" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/charadd" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_marginTop="10dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="65dp" android:layout_height="65dp" android:background="@drawable/selector" android:text="1" android:gravity="center" android:textSize="25sp" android:id="@+id/num1" /> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="2" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num2"/> <Button android:layout_width="65dp" android:layout_height="65dp" android:text="3" android:background="@drawable/selector" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/num3"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:id="@+id/num0" android:layout_width="140dp" android:layout_height="65dp" android:text="0" android:background="@drawable/selector" android:gravity="center" android:textSize="25sp" /> <Button android:id="@+id/dian" android:layout_width="65dp" android:layout_height="65dp" android:layout_marginLeft="10dp" android:background="@drawable/selector" android:gravity="center" android:text="." android:textSize="25sp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="65dp" android:layout_height="140dp" android:text="=" android:background="@drawable/equal" android:gravity="center" android:layout_marginLeft="10dp" android:textSize="25sp" android:id="@+id/equai"/> </LinearLayout> </LinearLayout> </LinearLayout>
java代码
package com.example.administrator.calculate; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.input) EditText input; @BindView(R.id.clear) Button clear; @BindView(R.id.delete) Button delete; @BindView(R.id.cheng) Button cheng; @BindView(R.id.clu) Button chu; @BindView(R.id.num7) Button num7; @BindView(R.id.num8) Button num8; @BindView(R.id.num9) Button num9; @BindView(R.id.charjian) Button charjian; @BindView(R.id.num4) Button num4; @BindView(R.id.num5) Button num5; @BindView(R.id.num6) Button num6; @BindView(R.id.charadd) Button charadd; @BindView(R.id.num1) Button num1; @BindView(R.id.num2) Button num2; @BindView(R.id.num3) Button num3; @BindView(R.id.num0) Button num0; @BindView(R.id.dian) Button dian; @BindView(R.id.equai) Button equal; private String ss=""; private boolean fu=false; private boolean num=false; private boolean point=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.clear, R.id.delete, R.id.cheng, R.id.clu, R.id.num7, R.id.num8, R.id.num9, R.id.charjian, R.id.num4, R.id.num5, R.id.num6, R.id.charadd, R.id.num1, R.id.num2, R.id.num3, R.id.num0, R.id.dian, R.id.equai}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.clear: { ss=""; input.setText(ss); } break; case R.id.delete: { if(ss.indexOf(" ")==ss.length()-3) { ss= ss.substring(0,ss.length() - 2); } if(ss.length()>0) { ss= ss.substring(0,ss.length() - 1); } input.setText(ss); } break; case R.id.cheng: { if(ss.length()==0) { break; } if(ss.contains(" ")) { if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break; getResult(); } fu=true; ss+=" × "; input.setText(ss); } break; case R.id.clu: { if(ss.length()==0) { break; } if(ss.contains(" ")) { if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break; getResult(); } fu=true; ss+=" ÷ "; input.setText(ss); } break; case R.id.num7: { ss+="7"; input.setText(ss); } break; case R.id.num8: { ss+="8"; input.setText(ss); } break; case R.id.num9: { ss+="9"; input.setText(ss); } break; case R.id.charjian: { if(ss.length()==0) { break; } if(ss.contains(" ")) { if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break; getResult(); } fu=true; ss+=" - "; input.setText(ss); } break; case R.id.num4: { ss+="4"; input.setText(ss); } break; case R.id.num5: { ss+="5"; input.setText(ss); } break; case R.id.num6: { ss+="6"; input.setText(ss); } break; case R.id.charadd: { if(ss.length()==0) { break; } if(ss.contains(" ")) { if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break; getResult(); } fu=true; ss+=" + "; input.setText(ss); } break; case R.id.num1: { ss+="1"; input.setText(ss); } break; case R.id.num2: { ss+="2"; input.setText(ss); } break; case R.id.num3: { ss+="3"; input.setText(ss); } break; case R.id.num0: { ss+="0"; input.setText(ss); } break; case R.id.dian: { if(ss.length()==0||ss.indexOf(" ")==ss.length()-3||ss.lastIndexOf(".")>ss.indexOf(" ")) { break; } else { ss+="."; input.setText(ss); } } break; case R.id.equai: getResult(); break; } } private void getResult() { double result=0; if(ss==null||ss.equals("")) return; if(!ss.contains(" ")) return; String s1=ss.substring(0,ss.indexOf(" ")); String op=ss.substring(ss.indexOf(" ")+1,ss.indexOf(" ")+2); String s2=ss.substring(ss.indexOf(" ")+3); if(!s1.equals("")&&!s2.equals("")) { double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2 ); switch (op) { case "+": result=d1+d2;break; case "-": result=d1-d2;break; case "×": result=d1*d2;break; case "÷": { if(d2==0) { Toast.makeText(this, "不能除以零", Toast.LENGTH_SHORT).show(); break; } result=d1/d2*1.0; } break; } int r = (int) result; if(r==result) { input.setText(""+r); ss=""+r; } else { input.setText(result+""); ss=""+result; } } } }
在AndroidManifest.xml文件中activity 后面添加
android:theme=”@style/Theme.AppCompat.DayNight.NoActionBar”
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)