android 开发 简单的小计算器

↑大致效果
项目构成:

随便写的,用的线性布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:orientation="vertical"
android:padding="5dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_marginBottom="20dp"
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
android:lines="3"
android:maxLines="3"
android:scrollbars="vertical"
android:textColor="#000000"
android:textSize="38dp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:showDividers="middle"> <Button
android:id="@+id/btn_cancel"
style="@style/btn_cal"
android:text="CE" /> <Button
android:id="@+id/btn_clear"
style="@style/btn_cal"
android:text="C" /> <Button
android:id="@+id/btn_sqrt"
style="@style/btn_cal"
android:text="√" /> <Button
android:id="@+id/btn_plus"
style="@style/btn_cal"
android:text="+" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:showDividers="middle"> <Button
android:id="@+id/btn_seven"
style="@style/btn_cal"
android:text="7" /> <Button
android:id="@+id/btn_eight"
style="@style/btn_cal"
android:text="8" /> <Button
android:id="@+id/btn_nine"
style="@style/btn_cal"
android:text="9" /> <Button
android:id="@+id/btn_minus"
style="@style/btn_cal"
android:text="-" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:showDividers="middle"> <Button
android:id="@+id/btn_four"
style="@style/btn_cal"
android:text="4" /> <Button
android:id="@+id/btn_five"
style="@style/btn_cal"
android:text="5" /> <Button
android:id="@+id/btn_six"
style="@style/btn_cal"
android:text="6" /> <Button
android:id="@+id/btn_multiply"
style="@style/btn_cal"
android:text="×" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:showDividers="middle"> <Button
android:id="@+id/btn_one"
style="@style/btn_cal"
android:text="1" /> <Button
android:id="@+id/btn_two"
style="@style/btn_cal"
android:text="2" /> <Button
android:id="@+id/btn_three"
style="@style/btn_cal"
android:text="3" /> <Button
android:id="@+id/btn_divide"
style="@style/btn_cal"
android:text="÷" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:showDividers="middle"> <Button
android:id="@+id/btn_zero"
style="@style/btn_cal"
android:layout_weight="2"
android:text="0" /> <Button
android:id="@+id/btn_dot"
style="@style/btn_cal"
android:text="." /> <Button
android:id="@+id/btn_equal"
style="@style/btn_cal"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Main_activiity类:
package com.example.myapplication6; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
double sum = 0.0;
double temp = 0.0;
int op = -1;
double step = 1;
boolean positive = true;
boolean flag = true;
TextView tv = null;
Button sqrt = null;
Button C = null;
Button CE = null;
Button dot = null;
Button num1 = null;
Button num2 = null;
Button num3 = null;
Button num4 = null;
Button num5 = null;
Button num6 = null;
Button num7 = null;
Button num8 = null;
Button num9 = null;
Button num0 = null;
Button plus = null;
Button subtract = null;
Button multiply = null;
Button divide = null;
Button equal = null;
NumUtil numUtil = new NumUtil();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView) findViewById(R.id.tv_result);
num0 = (Button) findViewById(R.id.btn_zero);
num0.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(temp < 0.00000001 && temp > 0.0)
tv.setText("0");
else{
if(flag){
temp *= 10;
}else {
step /= 10;
}
tv.setText(numUtil._tv(temp));
}
}
});
num1 = (Button) findViewById(R.id.btn_one);
num1.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 1;
}else {
step /= 10;
temp += step * 1.0;
}
tv.setText(numUtil._tv(temp));
}
});
num2 = (Button) findViewById(R.id.btn_two);
num2.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 2;
}else {
step /= 10;
temp += step * 2;
}
tv.setText(numUtil._tv(temp));
}
});
num3 = (Button) findViewById(R.id.btn_three);
num3.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 3;
}else {
step /= 10;
temp += step * 3;
}
tv.setText(numUtil._tv(temp));
}
});
num4 = (Button) findViewById(R.id.btn_four);
num4.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 4;
}else {
step /= 10;
temp += step * 4;
}
tv.setText(numUtil._tv(temp));
}
});
num5 = (Button) findViewById(R.id.btn_five);
num5.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 5;
}else {
step /= 10;
temp += step * 5;
}
tv.setText(numUtil._tv(temp));
}
});
num6 = (Button) findViewById(R.id.btn_six);
num6.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 6;
}else {
step /= 10;
temp += step * 6;
}
tv.setText(numUtil._tv(temp));
}
});
num7 = (Button) findViewById(R.id.btn_seven);
num7.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 7;
}else {
step /= 10;
temp += step * 7;
}
tv.setText(numUtil._tv(temp));
}
});
num8 = (Button) findViewById(R.id.btn_eight);
num8.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 8;
}else {
step /= 10;
temp += step * 8;
}
tv.setText(numUtil._tv(temp));
}
});
num9 = (Button) findViewById(R.id.btn_nine);
num9.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
temp *= 10;
temp += 9;
}else {
step /= 10;
temp += step * 9;
}
tv.setText(numUtil._tv(temp));
}
});
dot = (Button) findViewById(R.id.btn_dot);
dot.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
flag = false;
tv.setText(numUtil._tv(temp)+".");
}
}
});
equal = (Button) findViewById(R.id.btn_equal);
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(op == -1){
sum = temp;
}else if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
tv.setText(numUtil._tv(sum));
positive = true;
flag = true;
sum = 0.0;
temp = 0.0;
step = 1.0;
op = -1;
}
});
plus = (Button) findViewById(R.id.btn_plus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("+");
if(op == -1){
sum = temp;
temp = 0.0;
}else{
if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
}
op = 1;
temp = 0.0;
positive = true;
flag = true;
}
});
subtract = (Button) findViewById(R.id.btn_minus);
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("-");
if(op == -1){
sum = temp;
temp = 0.0;
}else{
if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
}
op = 2;
temp = 0.0;
positive = true;
flag = true;
}
});
multiply = (Button) findViewById(R.id.btn_multiply);
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("×");
if(op == -1){
sum = temp;
temp = 0.0;
}else{
if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
}
op = 3;
temp = 0.0;
positive = true;
flag = true;
}
});
divide = (Button) findViewById(R.id.btn_divide);
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("÷");
if(op == -1){
sum = temp;
temp = 0.0;
}else{
if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
}
op = 4;
temp = 0.0;
positive = true;
flag = true;
}
});
C = (Button) findViewById(R.id.btn_cancel);
C.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(" ");
positive = true;
flag = true;
temp = 0.0;
}
});
CE = (Button) findViewById(R.id.btn_clear);
CE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(" ");
positive = true;
flag = true;
sum = 0.0;
temp = 0.0;
step = 1.0;
op = -1;
}
});
sqrt = (Button) findViewById(R.id.btn_sqrt);
sqrt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(op == -1){
sum = temp;
}else if(op == 1){
sum += temp;
}else if(op == 2){
sum -= temp;
}else if(op == 3){
sum *= temp;
}else if(op == 4){
sum /= temp;
}
sum = Math.sqrt(sum);
tv.setText(numUtil._tv(sum));
positive = true;
flag = true;
sum = 0.0;
temp = 0.0;
step = 1.0;
op = -1;
}
});
} }
NumUtil工具类:
package com.example.myapplication6;
public class NumUtil {
public double toDouble(String str){
return 0.0;
}
public String _tv(double x){
double t = x - (int) x;
if(t < 0)
t = -t;
if(t < 0.00000001)
return String.valueOf((int)x);
else{
String temp = String.format("%.7f", x);
for(int i = temp.length() -1; i >= 0; i--){
if(temp.substring(i,i+1).equals("0")){
continue;
}else{
if(i + 1 < temp.length()){
temp = temp.substring(0,i+1);
}
break;
}
}
return temp;
}
}
}
按钮的垃圾设定
<style name="btn_cal">
<item name="android:layout_marginLeft">3dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#EEA9B8</item>
<item name="android:textSize">30px</item>
</style>
不太好弄的地方就是对于小数点后的处理了,剩下的随便写写就完事了
android 开发 简单的小计算器的更多相关文章
- Android开发效率的小技巧
提高eclipse使用效率(二) 提高Android开发效率的小技巧 XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Wi ...
- 几款Android开发人员必备小工具
在这里我介绍一下我常常在Android Studio里面使用的小工具吧,这些工具都能够在plugin里面搜索到. (当然了哈.我也是从网上找的.用着挺方便的,在这里总结一下) Gsonformat: ...
- 我对android开发的一点小感悟小看法
“Android”,“Android开发”等等这些词成了时下最热的词,也是时下大众最关注最吸引人眼球的话题,当然,最热门的行业也意味着高薪,好的就业环境,但同时也意味着强大的竞争力! Android系 ...
- 新人学习Android开发遇到的小问题总结
1. IDE搭建: 搭建android的IDE时,先注意是什么版本的系统,64/32位系统. 通常使用的是Eclipse for android,Android Studio由于还需要FQ,网速慢,所 ...
- Android开发中的小技巧
转自:http://blog.csdn.net/guxiao1201/article/details/40655661 简单介绍: startActivities (Intent[] intents) ...
- 提高eclipse使用效率(二) 提高Android开发效率的小技巧
XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...
- Android 开发中常用小技巧
TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSize() ...
- 提高eclipse使用效率(二)—— 提高Android开发效率的小技巧
XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...
- android 开发 简单的页面布局
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view ...
随机推荐
- eclipse报Access restriction: The type 'BASE64Decoder' is not API处理方法
今天从svn更新代码之后,由于代码中使用了BASE64Encoder 更新之后报如下错误: Access restriction: The type ‘BASE64Decoder’ is not A ...
- 前端开发之jQuery位置属性和筛选方法
主要内容: 1.jQuery的位置属性及实例 (1)位置属性 (2)实例 --- 仿淘宝导航栏 2.jQuery的筛选方法及实例 (1)筛选方法 (2)实例一:嵌套选项卡 (3)实例二:小米官网滑动 ...
- java文档打包成压缩包并且下载
需求,根据产品ID查询产品详情,产品详情会返回产品的一些文案,以及图片的url.需要做成,将文案信息记录在一个txt文档中,然后图片下载到文件夹,最后下载到本地,下载后自动删除刚才生成的文件夹以及文件 ...
- PHP与Imagemagick
Imagemagick:相关站点: ImageMagick中文站:http://www.imagemagick.com.cn/ ImageMagick英文站:http://www.imagemagic ...
- MySQL学习3---事务
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据. 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务. 事务处理可以用来维护数据库的完整性,保证成批的 ...
- (转)Java 中关于String的空对象(null) ,空值(empty),空格
原文出处:Java 中关于String的空对象(null) ,空值(empty),空格 定义 空对象: String s = null; 空对象是指定义一个对象s,但是没有给该对象分配空间,即没有实例 ...
- 基于Bind实现的DNS正反向解析及主从DNS的配置
一.什么是DNS? 1.1 简单的理解,Domain Name System,是互联网一项核心的服务,他作为一个桥梁可以将域名和IP地址相互因素的一个分布式数据库,能够使人更加方便的访问互联网,而不用 ...
- CI框架入门教程
1. URL常用的相关函数 url相关函数在辅助类url中第一,要使用它们必须先加载$this->load->helper('url')或者自动装载 site_url('控制器/方法 ...
- awk基础04-内置函数
在awk中常用的内置函数大概分为:数值函数.字符函数.时间函数.二进制操作函数.数组函数.自定义函数等. 数值函数 常用的数值函数主要有int.rand.srand.sqrt等.详细如下所 ...
- 装饰者模式及C++实现
装饰者模式 时常会遇到这样一种情况,我已经设计好了一个接口,并且也有几个实现类,但是这时我发现我设计的时候疏忽了,忘记了一些功能,或者后来需求变动要求加入一些功能,最简单的做法就是修改接口,添加函数, ...