前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Studio实现简易计算器

Android Studio实现简易计算器

作者头像
砸漏
发布2020-11-05 10:11:31
1.4K0
发布2020-11-05 10:11:31
举报
文章被收录于专栏:恩蓝脚本

如果是制作简易计算器的话是基本没有难点的,供大家参考,具体内容如下

步骤是先写好界面布局,将按钮的布局、字号颜色啥的做好,再就是设置监听器。

使用了NoTitleBar的主题

代码如下:

activity_main里关于界面的代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?  
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
<EditText
android:id="@+id/et_input"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="@drawable/white_bg"
android:editable="false"
android:gravity="right|bottom"
android:paddingBottom="20dp"
android:paddingRight="20dp"
android:textSize="50sp" / 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
<Button
android:id="@+id/bt_clr"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="C"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="DEL"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_div"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="÷"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_mul"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="×"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
<Button
android:id="@+id/bt_7"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="7"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_8"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="8"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_9"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="9"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_sub"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="-"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
<Button
android:id="@+id/bt_4"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="4"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_5"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="5"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_6"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="6"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:id="@+id/bt_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="+"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" 
<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="80dp"
android:layout_height="80dp"
android:id="@+id/bt_1"
android:text="1"
android:textSize="30sp"
android:gravity="right|bottom"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_2"
android:text="2"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_3"
android:text="3"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp" 
<Button
android:layout_width="170dp"
android:layout_height="80dp"
android:id="@+id/bt_0"
android:text="0"
android:textSize="30sp"
android:gravity="right|bottom"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_pt"
android:text="."
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
</LinearLayout 
<Button
android:id="@+id/bt_eq"
android:layout_width="80dp"
android:layout_height="170dp"
android:layout_marginLeft="10dp"
android:background="@drawable/orange_selector"
android:gravity="right|bottom"
android:text="="
android:textSize="30sp"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/ 
</LinearLayout 
</LinearLayout 

Mainactivity的代码:

代码语言:javascript
复制
package com.example.administrator.calculatordemo;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;
Button bt_mul,bt_div,bt_add,bt_sub;
Button bt_clr,bt_del,bt_eq;
EditText et_input;
boolean clr_flag; //判断et中是否清空
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化对象
setContentView(R.layout.activity_main);
bt_0= (Button) findViewById(R.id.bt_0);
bt_1= (Button) findViewById(R.id.bt_1);
bt_2= (Button) findViewById(R.id.bt_2);
bt_3= (Button) findViewById(R.id.bt_3);
bt_4= (Button) findViewById(R.id.bt_4);
bt_5= (Button) findViewById(R.id.bt_5);
bt_6= (Button) findViewById(R.id.bt_6);
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_pt= (Button) findViewById(R.id.bt_pt);
bt_add= (Button) findViewById(R.id.bt_add);
bt_sub= (Button) findViewById(R.id.bt_sub);
bt_mul= (Button) findViewById(R.id.bt_mul);
bt_div= (Button) findViewById(R.id.bt_div);
bt_clr= (Button) findViewById(R.id.bt_clr);
bt_del= (Button) findViewById(R.id.bt_del);
bt_eq= (Button) findViewById(R.id.bt_eq);
et_input= (EditText) findViewById(R.id.et_input);
//设置按钮的点击事件
bt_0.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_pt.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_sub.setOnClickListener(this);
bt_mul.setOnClickListener(this);
bt_div.setOnClickListener(this);
bt_clr.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_eq.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=et_input.getText().toString();
switch (v.getId()){
case R.id.bt_0:
case R.id.bt_1:
case R.id.bt_2:
case R.id.bt_3:
case R.id.bt_4:
case R.id.bt_5:
case R.id.bt_6:
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_pt:
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.bt_add:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_div:
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
str=str.substring(0,str.indexOf(" "));
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.bt_clr:
if(clr_flag)
clr_flag=false;
str="";
et_input.setText("");
break;
case R.id.bt_del: //判断是否为空,然后在进行删除
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
else if(str!=null&&!str.equals("")){
et_input.setText(str.substring(0,str.length()-1));
}
break;
case R.id.bt_eq: //单独运算最后结果
getResult();
break;
}
}
private void getResult(){
String exp=et_input.getText().toString();
if(exp==null||exp.equals("")) return ;
//因为没有运算符所以不用运算
if(!exp.contains(" ")){
return ;
}
if(clr_flag){
clr_flag=false;
return;
}
clr_flag=true;
//截取运算符前面的字符串
String s1=exp.substring(0,exp.indexOf(" "));
//截取的运算符
String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
//截取运算符后面的字符串
String s2=exp.substring(exp.indexOf(" ")+3);
double cnt=0;
if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
cnt=d1+d2;
}
if(op.equals("-")){
cnt=d1-d2;
}
if(op.equals("×")){
cnt=d1*d2;
}
if(op.equals("÷")){
if(d2==0) cnt=0;
else cnt=d1/d2;
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
//s1不为空但s2为空
else if(!s1.equals("")&&s2.equals("")){
double d1=Double.parseDouble(s1);
if(op.equals("+")){
cnt=d1;
}
if(op.equals("-")){
cnt=d1;
}
if(op.equals("×")){
cnt=0;
}
if(op.equals("÷")){
cnt=0;
}
if(!s1.contains(".")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
//s1是空但s2不是空
else if(s1.equals("")&&!s2.equals("")){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
cnt=d2;
}
if(op.equals("-")){
cnt=0-d2;
}
if(op.equals("×")){
cnt=0;
}
if(op.equals("÷")){
cnt=0;
}
if(!s2.contains(".")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
else {
et_input.setText("");
}
}
}

关于计算器的精彩文章请查看《计算器专题》 ,更多精彩等你来发现!

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档