四则运算第二次冲刺更新进度

四则运算第二次冲刺进度更新:

此次我们完善了计算器功能:

代码:MainActivity.java

package com.example.jisuanqi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

    Button btn_0;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_clear;
    Button btn_del;
    Button btn_divide;
    Button btn_multiply;
    Button btn_mimus;
    Button btn_plus;
    Button btn_point;
    Button btn_equal;
    EditText editText1;
    boolean clear_flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1=(EditText) findViewById(R.id.editText1);
        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_clear=(Button) findViewById(R.id.btn_clear);
        btn_del=(Button) findViewById(R.id.btn_del);
        btn_divide=(Button) findViewById(R.id.btn_divide);
        btn_equal=(Button) findViewById(R.id.btn_equal);
        btn_mimus=(Button) findViewById(R.id.btn_mimus);
        btn_multiply=(Button) findViewById(R.id.btn_multiply);
        btn_plus=(Button) findViewById(R.id.btn_plus);
        btn_point=(Button) findViewById(R.id.btn_point);
        
        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_clear.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_mimus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_point.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        String str=editText1.getText().toString();
        switch(arg0.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;
                editText1.setText("");
            }
            editText1.setText(str+((Button)arg0).getText());
            break;
        
        case R.id.btn_plus:
        case R.id.btn_mimus:
        case R.id.btn_multiply:
        case R.id.btn_divide:
            if(clear_flag)
            {
                clear_flag=false;
                editText1.setText("");
            }
            editText1.setText(str+((Button)arg0).getText()+" ");
            break;
            
        case R.id.btn_clear:
            clear_flag=false;
            editText1.setText("");
            break;
        case R.id.btn_del:
            if(clear_flag)
            {
                clear_flag=false;
                editText1.setText("");
            }
            else if(str!=null && str.equals(""))
            {
                editText1.setText(str.substring(0,str.length()-1));
            }
            break;
        case R.id.btn_equal:
            getResult();
            break;
        }
    }
    private void getResult()
    {
        
        String exp=editText1.getText().toString();
        if(exp==null || exp.equals(""))
        {
            return;
        }
        if(!exp.contains(" "))
        {
            return;
        }
        if(clear_flag)
        {
            clear_flag=false;
            return;
        }
        clear_flag=true;
        double result=0;
        String s1=exp.substring(0, exp.indexOf(" "));
        String op=exp.substring(exp.indexOf(" ")+1, exp.indexOf(" ")+2);
        String s2=exp.substring(exp.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("."))
            {
                int r=(int)result;
                editText1.setText(r+"");
            }
            else
            {
                editText1.setText(result+"");
            }
        }
        else if(!s1.equals("") && s2.equals(""))
        {
            editText1.setText(exp);
        }
        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(!s2.contains("."))
            {
                int r=(int)result;
                editText1.setText(r+"");
            }
            else
            {
                editText1.setText(result+"");
            }
        }else
        {
            editText1.setText("");
        }
    }
    
}

activity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:editable="false"
        android:gravity="right|bottom"
        android:background="@drawable/white_bg"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
         />
         <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="75dp"
             android:orientation="horizontal"
             android:gravity="center_horizontal"
             >
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_clear"
                 android:text="C"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_del"
                 android:layout_marginLeft="10dp"
                 android:text="DEL"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_divide"
                 android:text="÷"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_multiply"
                 android:text="×"
                 android:textSize="20sp"/>
         </LinearLayout>
           <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             android:layout_marginTop="145dp"
             android:gravity="center_horizontal"
             >
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_7"
                 android:text="7"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_8"
                 android:layout_marginLeft="10dp"
                 android:text="8"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_9"
                 android:text="9"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_mimus"
                 android:text="-"
                 android:textSize="20sp"/>
         </LinearLayout>
         <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="215dp"
             android:orientation="horizontal"
             android:gravity="center_horizontal"
             >
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_4"
                 android:text="4"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_5"
                 android:layout_marginLeft="10dp"
                 android:text="5"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_6"
                 android:text="6"
                 android:textSize="20sp"/>
             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:layout_marginLeft="10dp"
                 android:id="@+id/btn_plus"
                 android:text="+"
                 android:textSize="20sp"/>
         </LinearLayout>
         
         <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             android:gravity="center_horizontal"
             android:layout_marginTop="285dp"
             >
             <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="60dp"
                     android:layout_height="60dp"
                     android:gravity="right|bottom"
                     android:background="@drawable/white_selector"
                     android:paddingRight="10dp"
                     android:paddingBottom="10dp"
                     android:text="1"
                     android:textSize="20sp" />

                 <Button
                     android:id="@+id/btn_2"
                     android:layout_width="60dp"
                     android:layout_height="60dp"
                     android:layout_marginLeft="10dp"
                     android:gravity="right|bottom"
                     android:background="@drawable/white_selector"
                     android:paddingRight="10dp"
                     android:paddingBottom="10dp"
                     android:text="2"
                     android:textSize="20sp" />

             <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:layout_marginLeft="10dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_3"
                 android:text="3"
                 android:textSize="20sp"/>
             </LinearLayout>
             <LinearLayout 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
             android:orientation="horizontal">
               
                 <Button 
                 android:layout_width="60dp"
                 android:layout_height="60dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/white_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_point"
                 android:text="."
                 android:textSize="20sp"/>

                 <Button
                     android:id="@+id/btn_0"
                     android:layout_width="130dp"
                     android:layout_height="60dp"
                     android:layout_marginLeft="10dp"
                     android:gravity="right|bottom"
                     android:background="@drawable/white_selector"
                     android:paddingRight="10dp"
                      android:paddingBottom="10dp"
                     android:text="0"
                     android:textSize="20sp" />
  
             </LinearLayout> 
            </LinearLayout>
            <Button 
                 android:layout_width="60dp"
                 android:layout_height="130dp"
                 android:layout_marginLeft="10dp"
                 android:gravity="right|bottom"
                 android:background="@drawable/orange_selector"
                 android:paddingRight="10dp"
                 android:paddingBottom="10dp"
                 android:id="@+id/btn_equal"
                 android:text="="
                 android:textSize="20sp"/>
         </LinearLayout>
</RelativeLayout>

转载于:https://www.cnblogs.com/zzhuzi/p/5009728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值