赞
踩
- <?xml version="1.0" encoding="utf-8"?>
- <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:baselineAligned="false"
- android:background="@color/grey"
- android:columnCount="4"
- android:rowCount="6">
- <TextView
- android:id="@+id/textview_01"
- android:layout_columnSpan="4"
- android:layout_gravity="fill"
- android:gravity="right"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:background="@color/grey"
- android:paddingRight="20dp"
- android:paddingTop="40dp"
- android:text="0"
- android:layout_columnWeight="1"
- android:textSize="88sp">
- </TextView>
- <Button
- android:id="@+id/button_return"
- android:layout_columnSpan="2"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="2dp"
- android:background="@color/white"
- android:textSize="30dp"
- android:textStyle="bold"
- android:text="回退" />
- <Button
- android:id="@+id/button_clear"
- android:layout_columnSpan="2"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="2dp"
- android:background="@color/white"
- android:textSize="30dp"
- android:textStyle="bold"
- android:text="清空"/>
- <Button
- android:id="@+id/button_add"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="+"/>
- <Button
- android:id="@+id/button_01"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="1"/>
- <Button
- android:id="@+id/button_02"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="2"/>
- <Button
- android:id="@+id/button_03"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="3"/>
- <Button
- android:id="@+id/button_reduce"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="-"/>
- <Button
- android:id="@+id/button_04"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="4"/>
- <Button
- android:id="@+id/button_05"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="5"/>
- <Button
- android:id="@+id/button_06"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="6"/>
- <Button
- android:id="@+id/button_ride"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="*"/>
- <Button
- android:id="@+id/button_07"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="7"/>
- <Button
- android:id="@+id/button_08"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="8"/>
- <Button
- android:id="@+id/button_09"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="9"/>
- <Button
- android:id="@+id/button_except"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="/"/>
- <Button
- android:id="@+id/button_spot"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="."/>
- <Button
- android:id="@+id/button_00"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="0"/>
- <Button
- android:id="@+id/button_equal"
- android:layout_columnSpan="1"
- android:gravity="center"
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
- android:layout_margin="1dp"
- android:background="@color/white"
- android:textSize="50dp"
- android:text="="/>
- </GridLayout>
- package com.example.pyfcalculator;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
- TextView textView;
- Button button_return,button_00,button_01,button_02,button_03,
- button_04,button_05,button_06,button_07,button_08,button_09,button_add,
- button_clear,button_equal,button_except,button_reduce,button_ride,button_spot;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initViewAndListener();
- }
-
-
- //注册监听方法
- protected void initViewAndListener(){
- button_return=findViewById(R.id.button_return);
- button_00=findViewById(R.id.button_00);
- button_01=findViewById(R.id.button_01);
- button_02=findViewById(R.id.button_02);
- button_03=findViewById(R.id.button_03);
- button_04=findViewById(R.id.button_04);
- button_05=findViewById(R.id.button_05);
- button_06=findViewById(R.id.button_06);
- button_07=findViewById(R.id.button_07);
- button_08=findViewById(R.id.button_08);
- button_09=findViewById(R.id.button_09);
- button_add=findViewById(R.id.button_add);
- button_clear=findViewById(R.id.button_clear);
- button_equal=findViewById(R.id.button_equal);
- button_except=findViewById(R.id.button_except);
- button_reduce=findViewById(R.id.button_reduce);
- button_ride=findViewById(R.id.button_ride);
- button_spot=findViewById(R.id.button_spot);
-
- button_return.setOnClickListener(this);
- button_00.setOnClickListener(this);
- button_01.setOnClickListener(this);
- button_02.setOnClickListener(this);
- button_03.setOnClickListener(this);
- button_04.setOnClickListener(this);
- button_05.setOnClickListener(this);
- button_06.setOnClickListener(this);
- button_07.setOnClickListener(this);
- button_08.setOnClickListener(this);
- button_09.setOnClickListener(this);
- button_add.setOnClickListener(this);
- button_clear.setOnClickListener(this);
- button_equal.setOnClickListener(this);
- button_except.setOnClickListener(this);
- button_reduce.setOnClickListener(this);
- button_ride.setOnClickListener(this);
- button_spot.setOnClickListener(this);
-
- textView=findViewById(R.id.textview_01);
- }
-
- private String num_in_now;//记录当前结果
- private boolean flag=false;//记录计算器状态
- int now_num=0;
-
- @Override
- public void onClick(View view) {
-
- //获取文本框内容
- String currentText=textView.getText().toString();
-
- switch (view.getId()){
- case R.id.button_00:
- if ((textView.getText().toString().equals("0")))
- return;
- isflag("0");
- break;
- case R.id.button_01:
- isflag("1");
- break;
- case R.id.button_02:
- isflag("2");
- break;
- case R.id.button_03:
- isflag("3");
- break;
- case R.id.button_04:
- isflag("4");
- break;
- case R.id.button_05:
- isflag("5");
- break;
- case R.id.button_06:
- isflag("6");
- break;
- case R.id.button_07:
- isflag("7");
- break;
- case R.id.button_08:
- isflag("8");
- break;
- case R.id.button_09:
- isflag("9");
- break;
- case R.id.button_add:
- // textView.setText(textView.getText()+"+");
- now_num++;
- if(now_num>1){
- equal();
- }
- isflag("+");
- break;
- case R.id.button_except:
- // textView.setText(textView.getText()+"/");
- now_num++;
- if(now_num>1){
- equal();
- }
- isflag("/");
- break;
- case R.id.button_spot:
- // textView.setText(textView.getText()+".");
- isflag(".");
- break;
- case R.id.button_reduce:
- // textView.setText(textView.getText()+"-");
- now_num++;
- if(now_num>1){
- equal();
- }
- isflag("-");
- break;
- case R.id.button_ride:
- // textView.setText(textView.getText()+"*");
- now_num++;
- if(now_num>1){
- equal();
- }
- isflag("*");
- break;
- case R.id.button_clear:
- textView.setText("0");
- flag=false;
- now_num=0;
- break;
- case R.id.button_return:
- if(TextUtils.isEmpty(textView.getText())){
- return;
- }
- if (currentText.substring(0,currentText.length()-1).length()>0){
- textView.setText(currentText.substring(0,currentText.length()-1));
- }
- else{
- textView.setText("0");
- flag=false;
- now_num=0;
- }
- break;
- case R.id.button_equal:
- if(TextUtils.isEmpty(textView.getText())){
- return;
- }
- equal();
- flag=false;
- now_num=0;
- break;
- }
-
- }
-
- private void equal(){
- textView.setText(operation(textView.getText().toString()));
- }
-
- //运算方法
- private String operation(String string){
- int index;
- double num_1,num_2;
- if (string.contains("+")){
- index=string.indexOf("+");
- num_1= Double.parseDouble(string.substring(0,index));
- num_2= Double.parseDouble(string.substring(index+1,string.length()));
- return num_1+num_2+"";
- }
- if (string.contains("-")){
- index=string.indexOf("-");
- if(index==0){
- String string_2=string.substring(1,string.length());
- int index_2=string_2.indexOf("-");
- num_1= Double.parseDouble(string_2.substring(0,index_2));
- num_2= Double.parseDouble(string_2.substring(index_2+1,string_2.length()));
- return (-(num_1+num_2))+"";
- }else{
- num_1= Double.parseDouble(string.substring(0,index));
- num_2= Double.parseDouble(string.substring(index+1,string.length()));
- return num_1-num_2+"";
- }
-
- }
- if (string.contains("*")){
- index=string.indexOf("*");
- num_1= Double.parseDouble(string.substring(0,index));
- num_2= Double.parseDouble(string.substring(index+1,string.length()));
- return num_1*num_2+"";
- }
- if (string.contains("/")){
- index=string.indexOf("/");
- num_1= Double.parseDouble(string.substring(0,index));
- num_2= Double.parseDouble(string.substring(index+1,string.length()));
- return num_1/num_2+"";
- }
- return "error!!!";
- }
-
- //判断0要不要被替换 看flag的状态 输入数字
- private void isflag(String info){
- if (flag){
- textView.setText(textView.getText()+info);
- }else{
- if(info.equals(".")||info.equals("+")||info.equals("-")||info.equals("*")||info.equals("/")){
- textView.setText(textView.getText()+info);
- }else {
- textView.setText(info);
- }
- flag=true;
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。