屏幕截圖 2020-12-07 174008.png (414.71 KB, 下載次數(shù): 110)
下載附件
計(jì)算器的圖片
2020-12-7 17:40 上傳
jisuanqi.rar
(8.85 MB, 下載次數(shù): 41)
2020-12-7 17:37 上傳
點(diǎn)擊文件名下載附件
Android 下載積分: 黑幣 -5
<?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:layout_gravity="center"
android:background="@drawable/main"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<EditText
android:id="@+id/et_showview"
android:layout_width="350dip"
android:layout_height="100dip"
android:background="@drawable/whitebg"
android:editable="false"
android:gravity="bottom|right"
android:textSize="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dip"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="C"
android:textSize="20sp" />
<Button
android:id="@+id/btn_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="DEL"
android:textSize="20sp" />
<Button
android:id="@+id/btn_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="÷"
android:textSize="20sp" />
<Button
android:id="@+id/btn_multiply"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:text="×"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/btn_8"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/btn_9"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="9"
android:textSize="20sp" />
<Button
android:id="@+id/btn_minus"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="4"
android:textSize="20sp" />
<Button
android:id="@+id/btn_5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="5"
android:textSize="20sp" />
<Button
android:id="@+id/btn_6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="6"
android:textSize="20sp" />
<Button
android:id="@+id/btn_pluse"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="+"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="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:id="@+id/btn_1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="1"
android:textSize="20sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="2"
android:textSize="20sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="3"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="horizontal">
<Button
android:id="@+id/btn_0"
android:layout_width="170dp"
android:layout_height="80dp"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/btn_point"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dip"
android:background="@drawable/white_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="."
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_equal"
android:layout_width="80dp"
android:layout_height="170dip"
android:layout_marginLeft="10dip"
android:background="@drawable/orange_btn_selector"
android:gravity="bottom|right"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="="
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.example;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_0;
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private Button btn_6;
private Button btn_7;
private Button btn_8;
private Button btn_9;
private Button btn_point;// 小數(shù)點(diǎn)
private Button btn_divide;// 除以
private Button btn_multiply;// 乘以
private Button btn_minus;// 減去
private Button btn_pluse;// 加
private Button btn_equal;// 等于
private Button btn_clear; //清空
private Button btn_del; //取消
private EditText et_showview; //輸入框
private double dou = 0; //接收結(jié)果
/**
* 加一個(gè)標(biāo)識(shí)
* 需求就是我們運(yùn)算出結(jié)果的時(shí)候,再次點(diǎn)擊數(shù)字及你選哪個(gè)下一次運(yùn)算,這個(gè)時(shí)候,輸入框應(yīng)該自動(dòng)為空
*/
private boolean flag; //清空標(biāo)識(shí)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);// 小數(shù)點(diǎn)
btn_divide = (Button) findViewById(R.id.btn_divide);// 除以
btn_multiply = (Button) findViewById(R.id.btn_multiply);// 乘以
btn_minus = (Button) findViewById(R.id.btn_minus);// 減去
btn_pluse = (Button) findViewById(R.id.btn_pluse);// 加
btn_equal = (Button) findViewById(R.id.btn_equal);// 等于
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
et_showview = (EditText) findViewById(R.id.et_showview);
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_point.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_pluse.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//我們?cè)谶@里實(shí)現(xiàn)業(yè)務(wù)邏輯
String str = et_showview.getText().toString();
switch (v.getId()) {
//我們點(diǎn)擊數(shù)字鍵,輸入框就收到我們的計(jì)算數(shù)字
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:
//再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
}
//我們只要點(diǎn)擊鍵盤,相應(yīng)的數(shù)字添加在EditText上
et_showview.setText(str + ((Button) v).getText());
break;
//運(yùn)算符也是一樣需要添加的
case R.id.btn_pluse://+
case R.id.btn_minus://-
case R.id.btn_multiply://*
case R.id.btn_divide:///
//再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
}
//為了計(jì)算方便,我們可以在前后添加空格
et_showview.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.btn_del:
//再次輸入的時(shí)候判斷,把前面的清空,再把點(diǎn)擊的按鈕放在輸入框上
if (flag) {
flag = false;
str = "";
et_showview.setText("");
} else if (str != null && !str.equals(" ")) {//判斷,因?yàn)槲覀冃枰粋(gè)個(gè)刪,首先確定是不是空或者""
//用substring()截取字符長(zhǎng)度-1 再設(shè)置回去 形成刪一個(gè)的效果
et_showview.setText(str.substring(0, str.length() - 1));
}
break;
//清除按鈕,直接把輸入框設(shè)置成空
case R.id.btn_clear:
flag = false;
str = "";
et_showview.setText(" ");
break;
//計(jì)算結(jié)果
case R.id.btn_equal:
getResult();
break;
}
}
//等號(hào)計(jì)算結(jié)果
private void getResult() {
//首先取一下你輸入完后現(xiàn)在輸入框的內(nèi)容
String result = et_showview.getText().toString();
//當(dāng)我們的輸入框是null或者""時(shí)我們不進(jìn)行操作
if (result == null || result.equals(" ")) {
return;
}
//我們要計(jì)算結(jié)果,必須知道輸入框是否有操作符,判斷條件就是前后是否在空格,否則不進(jìn)行操作
if (!result.contains(" ")) {
//沒有運(yùn)算符,所以不用運(yùn)算
return;
}
//當(dāng)我點(diǎn)擊等號(hào)的時(shí)候,清空標(biāo)識(shí)設(shè)置為true,但是如果之前有的話,設(shè)置成false
if (flag) {
flag = false;
return;
}
flag = true;
//如果有空格,我們就截取前后段再獲取運(yùn)算符進(jìn)行計(jì)算
String str1 = result.substring(0, result.indexOf(" ")); //運(yùn)算符前面字段
String op = result.substring(result.indexOf(" ") + 1, result.indexOf(" ") + 2); //截取運(yùn)算符
String str2 = result.substring(result.indexOf(" ") + 3); //運(yùn)算符后面字段
//這里我們還是要判斷str1和str2都不是空才
if (!str1.equals("") && !str2.equals("")) { //第一種情況:都不是空
double d1 = Double.parseDouble(str1);
double d2 = Double.parseDouble(str2);
//開始計(jì)算,定義一個(gè)double變量接收結(jié)果,定義為全局
if (op.equals("+")) { //加
dou = d1 + d2;
} else if (op.equals("-")) { //減
dou = d1 - d2;
} else if (op.equals("×")) { //乘
dou = d1 * d2;
} else if (op.equals("÷")) { //除
//除數(shù)為0不計(jì)算
if (d2 == 0) {
dou = 0;
} else {
dou = d1 / d2;
}
}
//我們之前把他強(qiáng)轉(zhuǎn)為double類型了,但是如果沒有小數(shù)點(diǎn),我們就是int類型
if (!str1.contains(".") && !str2.contains(".") && !op.equals("÷")) {
int i = (int) dou;
et_showview.setText(i + "");
} else { //如果有小數(shù)點(diǎn)
et_showview.setText(dou + "");
}
} else if (!str1.equals("") && str2.equals("")) { //第二種情況:str2是空
//這種情況就不運(yùn)算了
et_showview.setText(result);
} else if (str1.equals("") && !str2.equals("")) { //第三種情況:str1是空
//這種情況我們就需要判斷了,因?yàn)榇藭r(shí)str1 = 0;
double d3 = Double.parseDouble(str2);
if (op.equals("+")) { //加
dou = 0 + d3;
} else if (op.equals("-")) { //減
dou = 0 - d3;
} else if (op.equals("×")) { //乘
dou = 0;
} else if (op.equals("÷")) { //除
//除數(shù)為0不計(jì)算
dou = 0;
}
//我們之前把他強(qiáng)轉(zhuǎn)為double類型了,但是如果沒有小數(shù)點(diǎn),我們就是int類型
if (!str2.contains(".")) {
int i = (int) dou;
et_showview.setText(i + "");
} else { //如果有小數(shù)點(diǎn)
et_showview.setText(dou + "");
}
} else { //最后一種情況,他們兩個(gè)都是空
//我們清空掉
et_showview.setText("");
}
}
}
|