androidstudio如何实现计算器
这篇文章主要介绍android studio如何实现计算器,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
成都创新互联主营永州网站建设的网络公司,主营网站建设方案,app开发定制,永州h5微信小程序搭建,永州网站营销推广欢迎永州等地区企业咨询
Android是什么
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
具体内容如下
效果图:
资源文件:
color.xml
#3F51B5 #303F9F #FF4081 #FFFFFF #000000 #FFFFFF #BEBEBE #9AFF9A #F0FFFF
white.xml
设置input text的填充色为白色
selector.xml
点击按钮时产生阴影效果
equeal.xml
同理,等号的阴影效果
布局文件
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”
以上是“android studio如何实现计算器”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享名称:androidstudio如何实现计算器
网页链接:http://pwwzsj.com/article/ipddhp.html