日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 坐标点计算器,Android实现简易计算器

發布時間:2023/12/1 Android 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 坐标点计算器,Android实现简易计算器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開之前我還是想問問老師,為什么一定要星期天前交作業呢?由于條件限制,作品是趕出來的不是細細琢磨出來的。所以在這版apk中功能較為簡易,有待后期再不斷更新與優化

總體效果圖如下

布局activity_main.xml部分代碼

功能代碼MainActivity.java

package com.example.cal;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

import android.view.View;

import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,

btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint;

TextView text;

String str = "";

boolean clr_flag;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn0 = (Button) findViewById(R.id.btn0);

btn1 = (Button) findViewById(R.id.btn1);

btn2 = (Button) findViewById(R.id.btn2);

btn3 = (Button) findViewById(R.id.btn3);

btn4 = (Button) findViewById(R.id.btn4);

btn5 = (Button) findViewById(R.id.btn5);

btn6 = (Button) findViewById(R.id.btn6);

btn7 = (Button) findViewById(R.id.btn7);

btn8 = (Button) findViewById(R.id.btn8);

btn9 = (Button) findViewById(R.id.btn9);

btnClear = (Button) findViewById(R.id.btnClear);

btnPlus = (Button) findViewById(R.id.btnPlus);

btnSubtract = (Button) findViewById(R.id.btnSubtract);

btnMultiply = (Button) findViewById(R.id.btnMultiply);

btnDivide = (Button) findViewById(R.id.btnDivide);

btnPoint = (Button) findViewById(R.id.btnPoint);

btnSum = (Button) findViewById(R.id.btnSum);

text = (TextView) findViewById(R.id.text) ;

btn0.setOnClickListener(this);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

btn3.setOnClickListener(this);

btn4.setOnClickListener(this);

btn5.setOnClickListener(this);

btn6.setOnClickListener(this);

btn7.setOnClickListener(this);

btn8.setOnClickListener(this);

btn9.setOnClickListener(this);

btnClear.setOnClickListener(this);

btnPlus.setOnClickListener(this);

btnSubtract.setOnClickListener(this);

btnMultiply.setOnClickListener(this);

btnDivide.setOnClickListener(this);

btnPoint.setOnClickListener(this);

btnSum.setOnClickListener(new click());

}

public void onClick(View v) {

String input=text.getText().toString();

switch (v.getId()){

case R.id.btn0:

case R.id.btn1:

case R.id.btn2:

case R.id.btn3:

case R.id.btn4:

case R.id.btn5:

case R.id.btn6:

case R.id.btn7:

case R.id.btn8:

case R.id.btn9:

case R.id.btnPoint:

if(clr_flag){

clr_flag=false;

str="";

text.setText("");

}

text.setText(input+((Button)v).getText());

break;

case R.id.btnPlus:

case R.id.btnSubtract:

case R.id.btnMultiply:

case R.id.btnDivide:

if(clr_flag){

clr_flag=false;

input="";

text.setText("");

}

text.setText(input + " " + ((Button)v).getText() + " ");

break;

case R.id.btnClear:

text.setText("");

break;

}

}

class click implements OnClickListener{

public void onClick (View v) {

getResult();

}

}

private void getResult () {

String str1 = text.getText().toString();

if(str1 == null || str1.equals("")){

return;

}

if(!str1.contains(" ")){

return ;

}

if(clr_flag){

clr_flag=false;

return;

}

clr_flag=true;

double result = 0;

String s1 = str1.substring(0,str1.indexOf(" "));

String op = str1.substring(str1.indexOf(" ")+1,str1.indexOf(" ")+2);

String s2 = str1.substring(str1.indexOf(" ")+3);

if(!s1.equals("")&&!s2.equals("")) {

double d1 = Double.parseDouble(s1);

double d2 = Double.parseDouble(s2);

if (op.equals("+")) {

result = d1 + d2;

} else if (op.equals("-")) {

result = d1 - d2;

} else if (op.equals("*")) {

result = d1 * d2;

} else if (op.equals("/")) {

if (d2 == 0) {

result = 0;

}

else {

result = d1 / d2;

}

}

if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {

int r = (int) result;

text.setText(str1 + " = " + r + "");

} else{

text.setText(str1 + " = " + result + "");

}

}

//

else if(!s1.equals("")&&s2.equals("")){

double d1 = Double.parseDouble(s1);

if (op.equals("+")){

result = d1;

}

if (op.equals("-")) {

result = d1;

}

if (op.equals("*")) {

result = 0;

}

if (op.equals("/")) {

result = 0;

}

if(!s1.contains(".")) {

int res = (int) result;

text.setText(str1 + " = " + res+"");

}else {

text.setText(str1 + " = " + result+"");

}

}

else if(s1.equals("")&& !s2.equals("")){

double d2 = Double.parseDouble(s2);

if(op.equals("+")){

result = 0+d2;

}else if(op.equals("-")){

result = 0-d2;

}else if(op.equals("*")){

result = 0;

}else if(op.equals("/")){

result = 0;

}

if(!s1.contains(".") && !s2.contains(".")){

int res = (int) result;

text.setText(str1 + " = " + res + "");

}else{

text.setText(str1 + " = " + result + "");

}

}else{

text.setText("");

}

}

}

設計思路

1、頁面設計,采用網格布局GridLayout。用android:columnCount,android:rowCount,來設置行數和列數。android:layout_columnSpan,和android:layout_rowSpan用來指定該單元格占據的列數和行數。顏色的話根據個人喜好設置,在此用的是十六進制rgb,更多顏色選取可參考RBG顏色對照表http://tool.oschina.net/commons?type=3? ?關于GridLayout可參考GridLayout 使用總結簡書https://www.jianshu.com/p/2488847f9013

2、功能設計,總體思路是給每個鍵添加監聽器,獲取里面的值進行相應的加減乘除計算。計算過程和結果顯示在一個testview上

測試案例

總結反思

1.本次實驗主要是通過完成一個簡易計算器來練習布局與算法。

2.該項目中java部分代碼較簡易可以改進的地方有

(1)添加backspace按鍵,免得一次輸錯就直接全部清空

(2)小數部分的計算仍然存在一些小問題。如1.1+2.2=3.300000000000003

(3)當計算數字過大,占用地方變大會把計算部分的按鍵往下推下去。(屬于布局的問題)

本次反思到此結束,希望下次有更加充足的時間來優化代碼

總結

以上是生活随笔為你收集整理的android 坐标点计算器,Android实现简易计算器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。