




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】AndroidStudio如何实现简易计算器
这篇文章给大家分享的是有关AndroidStudio如何实现简易计算器的内容。在下觉得挺实用的,因此分享给大家做个参考,一起跟随在下过来看看吧。Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。这是一个运用网格布局来做的简易计算器,可能没有那么美观,大家可以继续完善首先先看看成果吧首先先建一个新的ProjectCalculator然后先编写颜色背景文件创建一个gray.xml,哪里创建呢?如图在drawable下右击,选择new–Drawableresourcefile第一个是文件名字,第二个属性可以自己选择,我们这里前两个文件选择shape,第三个文件选selector,附上颜色背景代码gray.xml<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android">
<corners
android:radius="5dp"/>
<solid
android:color="#f9f9f9"/>
<stroke
android:width="2dp"
android:color="#ffa600"/>
</shape>orange.xml<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android">
<corners
android:radius="5dp"/>
//
圆角
<solid
android:color="#F7B684"/>
//颜色
</shape>white.xml<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android">
<corners
android:radius="5dp"/>
<solid
android:color="#ffffff"/>
<stroke
android:width="1dp"
android:color="#ffa600"/>
</shape>change.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:drawable="@drawable/gray"/>
//默认颜色
<item
android:drawable="@drawable/orange"
android:state_pressed="true"/>
//按下的改变的颜色
</selector>这个是当你按下按键的时候按键会改变颜色接下来就是布局文件了activity_main.xml我用的是表格布局,大家也可以用表格布局来写,效果会好一些<?xml
version="1.0"
encoding="utf-8"?>
<TableLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8ECF3">
<TextView
android:gravity="bottom|right"
android:textSize="70dp"
android:singleLine="true"
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@drawable/white"
android:id="@+id/textView"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_clean"
android:layout_marginLeft="10dp"
android:background="@drawable/orange"
android:gravity="center"
android:text="C"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_del"
android:layout_marginLeft="10dp"
android:layout_span="2"
android:background="@drawable/gray"
android:gravity="center"
android:text="Del"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_divide"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/gray"
android:gravity="center"
android:layout_span="1"
android:text="/"
android:textSize="25sp"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_7"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="7"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_8"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="8"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_9"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="9"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_multiply"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/gray"
android:gravity="center"
android:text="*"
android:textSize="25sp"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_4"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="4"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_5"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="5"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_6"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="6"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_add"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/gray"
android:gravity="center"
android:text="+"
android:textSize="25sp"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_1"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="1"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_2"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="2"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_3"
android:layout_marginLeft="10dp"
android:background="@drawable/white"
android:gravity="center"
android:text="3"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_minus"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/gray"
android:gravity="center"
android:text="-"
android:textSize="25sp"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_0"
android:layout_marginLeft="10dp"
android:layout_span="2"
android:background="@drawable/white"
android:gravity="center"
android:text="0"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_point"
android:layout_marginLeft="10dp"
android:layout_span="1"
android:background="@drawable/white"
android:gravity="center"
android:text="."
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_equal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_span="1"
android:background="@drawable/gray"
android:gravity="center"
android:text="="
android:textSize="25sp"
/>
</TableRow>
</TableLayout>接下来就是MainActivity.javapackage
com.example.calculator;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener
{
Button
btn_clean,btn_del,btn_divide,btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,
btn_multiply,btn_add,btn_minus,btn_point,btn_equal;
TextView
textView;
boolean
clear_flag;
//清空标识
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate
(savedInstanceState);
setContentView
(R.layout.activity_main);
btn_0
=
findViewById(R.id.btn_0);
//初始化
btn_1
=
findViewById(R.id.btn_1);
btn_2
=
findViewById(R.id.btn_2);
btn_3
=
findViewById(R.id.btn_3);
btn_4
=
findViewById(R.id.btn_4);
btn_5
=
findViewById(R.id.btn_5);
btn_6
=
findViewById(R.id.btn_6);
btn_7
=
findViewById(R.id.btn_7);
btn_8
=
findViewById(R.id.btn_8);
btn_9
=
findViewById(R.id.btn_9);
btn_multiply
=
findViewById(R.id.btn_multiply);
btn_divide
=
findViewById(R.id.btn_divide);
btn_add
=
findViewById(R.id.btn_add);
btn_minus
=
findViewById(R.id.btn_minus);
btn_point
=
findViewById(R.id.btn_point);
btn_del
=findViewById(R.id.btn_del);
btn_equal
=
findViewById(R.id.btn_equal);
btn_clean
=
findViewById(R.id.btn_clean);
textView
=
findViewById(R.id.textView);
btn_0.setOnClickListener(this);
//设置按钮的点击事件
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clean.setOnClickListener(this);
}
public
void
onClick(View
v)
{
String
str
=
textView.getText().toString();
switch(v.getId
()){
case
R.id.btn_0:
case
R.id.btn_1:
case
R.id.btn_2:
case
R.id.btn_3:
case
R.id.btn_4:
case
R.id.btn_5:
case
R.id.btn_6:
case
R.id.btn_7:
case
R.id.btn_8:
case
R.id.btn_9:
case
R.id.btn_point:
if(clear_flag){
clear_flag=false;
str="";
textView.setText
("");
}
textView.setText(str+((Button)v).getText
());
break;
case
R.id.btn_add:
case
R.id.btn_minus:
case
R.id.btn_multiply:
case
R.id.btn_divide:
if(clear_flag){
clear_flag=false;
textView.setText("");
}
textView.setText(str+"
"+((Button)v).getText()+"
");
break;
case
R.id.btn_del:
if(clear_flag){
clear_flag=false;
textView.setText
("");
}else
if
(str
!=
null
&&
!str.equals
("")){
textView.setText(str.substring(0,str.length()-1));
//删除一个字符
}
break;
case
R.id.btn_clean:
clear_flag=false;
str
=
"";
textView.setText("");
//清空文本内容
break;
case
R.id.btn_equal:
getResult();
//获取结果
break;
}
}
private
void
getResult()
{
//算法
String
s
=
textView.getText().toString();
if(s
==
null
||
s.equals
("")){
return;
}
if
(!s.contains
("")){
return;
}
if
(clear_flag){
clear_flag=false;
return;
}
clear_flag=true;
String
str1
=
s.substring(0,s.indexOf("
"));
//
获取到运算符前面的字符
String
str_y
=
s.substring(s.indexOf("
")+1,s.indexOf("
")+2);
//获取到运算符
String
str2
=
s.substring(s.indexOf("
")+
3);
//获取到运算符后面的字符
double
result
=
0;
if
(!str1.equals
("")
&&
!str2.equals
("")){
double
num1
=
Double.parseDouble(str1);
//将str1、str2强制转化为double类型
doub
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO 17987-7:2025 EN Road vehicles - Local Interconnect Network (LIN) - Part 7: Electrical physical layer (EPL) conformance test specification
- 学校天然气使用协议书
- 成都安置房合同协议书
- 智能家居品牌及协议书
- 贝米钱包协议书
- 瓶装水生产转让协议书
- 毕业实习第三方协议书
- 终端购机协议书
- 排雷班主播签约协议书
- 快递员承包合同协议书
- GB/T 9115.1-2000平面、突面对焊钢制管法兰
- 教辅资料进校园审批制度
- 2023年广东省初中生物地理学业考试真题集合试卷及答案高清版汇总
- 人教版小学英语各年级重点单词、短语及句型
- 玖玖电玩城消防安全管理制度
- 烟道内喷涂施工方案
- 水电站运维管理
- 材料的断裂(1)
- 被子植物门分科检索表
- 监理预验收表格(共11页)
- 中国电信移动终端营销策略
评论
0/150
提交评论