简单计算器练习

看了一段时间的Android,跟着视频老师做了一个小的简单计算器来练习一下基本控件的使用和基本布局,没什么技术含量:

视图如下:


layout代码:

<LinearLayout 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:orientation="vertical"
    android:background="#f0f0f0"
    >

    <EditText 
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="5dp"
        android:textSize="30sp"
        android:editable="false"
        android:gravity="right|bottom"
        android:background="@drawable/edt_style"
        />
    
    <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_marginTop="10dp"
    	android:orientation="horizontal"
    	android:layout_gravity="center_horizontal"
        >
        
        <Button 
            android:id="@+id/bt_clear"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="C"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_divide"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="÷"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_multiple"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="×"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_delete"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="DEL"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_marginTop="10dp"
    	android:orientation="horizontal"
    	android:layout_gravity="center_horizontal"
        >
        
        <Button 
            android:id="@+id/bt_7"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="7"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_8"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="8"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_9"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="9"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_minus"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="-"
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_marginTop="10dp"
    	android:orientation="horizontal"
    	android:layout_gravity="center_horizontal"
        >
        
        <Button 
            android:id="@+id/bt_4"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="4"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_5"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="5"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_6"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:text="6"            
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
        <Button 
            android:id="@+id/bt_add"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="+"
            android:gravity="center"
            android:background="@drawable/bt_selector"
            
            />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_marginTop="10dp"
    	android:orientation="horizontal"
    	android:layout_gravity="center_horizontal"
        >
        
        <LinearLayout 
	        android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:layout_marginTop="10dp"
	    	android:orientation="vertical"
	    	android:layout_gravity="center_horizontal"
        	>
        	
            <LinearLayout 
		        android:layout_width="wrap_content"
		    	android:layout_height="wrap_content"
		    	android:orientation="horizontal"
		    	android:layout_gravity="center_horizontal"
		        >
		        <Button 
		            android:id="@+id/bt_1"
		            android:layout_width="60dp"
		            android:layout_height="60dp"
		            android:layout_marginLeft="10dp"
		            android:text="1"
		            android:gravity="center"
		            android:background="@drawable/bt_selector"
		            
		            />
		        <Button 
		            android:id="@+id/bt_2"
		            android:layout_width="60dp"
		            android:layout_height="60dp"
		            android:layout_marginLeft="10dp"
		            android:text="2"
		            android:gravity="center"
		            android:background="@drawable/bt_selector"
		            
		            />
		        
		    </LinearLayout>
		    <LinearLayout 
		        android:layout_width="wrap_content"
		    	android:layout_height="wrap_content"
		    	android:layout_marginTop="10dp"
		    	android:orientation="horizontal"
		    	android:layout_gravity="center_horizontal"
		        >
		        
		        <Button 
		            android:id="@+id/bt_0"
		            android:layout_width="130dp"
		            android:layout_height="60dp"
		            android:layout_marginLeft="10dp"
		            android:text="0"
		            android:gravity="center"
		            android:background="@drawable/bt_selector"
		            
		            />
		    </LinearLayout>
        
    	</LinearLayout>
    	<LinearLayout 
	        android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:layout_marginTop="10dp"
	    	android:orientation="vertical"
	    	android:layout_gravity="center_horizontal"
	        >
        	
        	<Button 
		    	android:id="@+id/bt_3"
		        android:layout_width="60dp"
		        android:layout_height="60dp"
		        android:layout_marginLeft="10dp"
		        android:text="3"
		        android:gravity="center"
		        android:background="@drawable/bt_selector"
		            
		        />
        	<Button 
		        android:id="@+id/bt_dot"
		        android:layout_width="60dp"
		        android:layout_height="60dp"
		        android:layout_marginLeft="10dp"
		        android:layout_marginTop="10dp"
		        android:text="."
		        android:gravity="center"
		        android:background="@drawable/bt_selector"
		            
		        />
    	</LinearLayout>
    	<LinearLayout 
	        android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:layout_marginTop="10dp"
	    	android:orientation="horizontal"
	    	android:layout_gravity="center_horizontal"
	        >
        
        	<Button 
            android:id="@+id/bt_equ"
            android:layout_width="60dp"
            android:layout_height="130dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="="
            android:gravity="center"
            android:background="@drawable/bt1_selector"
            
            />
    	</LinearLayout>
    </LinearLayout>

</LinearLayout>

drawable文件夹下面的文件代码:

bt_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners 
        android:radius="5dp"
        />
	<solid 
        android:color="#aaffaa"
        />

</shape>

bted_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners 
        android:radius="5dp"
        />
	<solid 
        android:color="#ccffaa"
        />

</shape>

bt_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/bted_bg" android:state_pressed="true"/>
    <item android:drawable="@drawable/bt_bg"/>

</selector>

bt1_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners 
        android:radius="5dp"
        />
	<solid 
        android:color="#eeee00"
        />

</shape>

bt1ed_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners 
        android:radius="5dp"
        />
	<solid 
        android:color="#daa520"
        />

</shape>

bt1_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/bt1ed_bg" android:state_pressed="true"/>
    <item android:drawable="@drawable/bt1_bg"/>

</selector>

edt_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners 
        android:radius="8dp"
        />
    <stroke
        android:width="1dp" 
        android:color="#9900ff"        
        />
    <solid 
        android:color="@android:color/white"
        />

</shape>

java代码:

package com.example.calculatedemo;

import com.example.calculatedemo.R.id;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity implements OnClickListener{

	private EditText edt1;
	private Button bt_0;
	private Button bt_1;
	private Button bt_2;
	private Button bt_3;
	private Button bt_4;
	private Button bt_5;
	private Button bt_6;
	private Button bt_7;
	private Button bt_8;
	private Button bt_9;
	private Button bt_dot;
	private Button bt_clear;
	private Button bt_delete;
	private Button bt_multiple;
	private Button bt_divide;
	private Button bt_add;
	private Button bt_minus;
	private Button bt_equ;
	
	private boolean clear_flag=false;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edt1 = (EditText) findViewById(R.id.edt1);
        
        bt_0 = (Button) findViewById(R.id.bt_0);
        bt_1 = (Button) findViewById(R.id.bt_1);
        bt_2 = (Button) findViewById(R.id.bt_2);
        bt_3 = (Button) findViewById(R.id.bt_3);
        bt_4 = (Button) findViewById(R.id.bt_4);
        bt_5 = (Button) findViewById(R.id.bt_5);
        bt_6 = (Button) findViewById(R.id.bt_6);
        bt_7 = (Button) findViewById(R.id.bt_7);
        bt_8 = (Button) findViewById(R.id.bt_8);
        bt_9 = (Button) findViewById(R.id.bt_9);
        bt_dot = (Button) findViewById(R.id.bt_dot);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_delete = (Button) findViewById(R.id.bt_delete);
        bt_add = (Button) findViewById(R.id.bt_add);
        bt_minus = (Button) findViewById(R.id.bt_minus);
        bt_multiple = (Button) findViewById(R.id.bt_multiple);
        bt_divide = (Button) findViewById(R.id.bt_divide);
        bt_equ = (Button) findViewById(R.id.bt_equ);
        
        bt_0.setOnClickListener(this);
        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);
        bt_9.setOnClickListener(this);
        bt_dot.setOnClickListener(this);
        bt_add.setOnClickListener(this);
        bt_minus.setOnClickListener(this);
        bt_multiple.setOnClickListener(this);
        bt_divide.setOnClickListener(this);
        bt_equ.setOnClickListener(this);
        bt_clear.setOnClickListener(this);
        bt_delete.setOnClickListener(this);
        
    }

	@Override
	public void onClick(View v) {

		String str = edt1.getText().toString();
		
		switch (v.getId()) {
		case R.id.bt_0:
		case R.id.bt_1:
		case R.id.bt_2:
		case R.id.bt_3:
		case R.id.bt_4:
		case R.id.bt_5:
		case R.id.bt_6:
		case R.id.bt_7:
		case R.id.bt_8:
		case R.id.bt_9:
		case R.id.bt_dot:
			if(clear_flag){
				clear_flag = false;
				edt1.setText("");
				str = "";
			}
			edt1.setText(str+((Button)v).getText());
			edt1.setSelection(edt1.length());
			break;
			
		case R.id.bt_add:
		case R.id.bt_multiple:
		case R.id.bt_minus:
		case R.id.bt_divide:
			if(clear_flag){
				clear_flag = false;
				edt1.setText("");
				str = "";
			}
			edt1.setText(str+" "+((Button)v).getText()+" ");
			edt1.setSelection(edt1.length());
			break;
			
		case R.id.bt_delete:
			if(clear_flag){
				clear_flag = false;
				edt1.setText("");
				str = "";
			}
			else if(str !=null && !str.equals("")){
				edt1.setText(str.substring(0,str.length()-1));
				edt1.setSelection(edt1.length());
			}
			break;
			
		case R.id.bt_clear:
			clear_flag = false;
			edt1.setText("");
			break;
			
		case R.id.bt_equ:
			calculate();
			break;
		}
	}
	
	public void calculate(){
		
		String exp = edt1.getText().toString();
		
		if(exp==null||exp.equals("")){
			return ;
		}
		if(!exp.contains(" ")){
			return ;
		}
		if(!judge()){
			edt1.setText("");
			return ;
		}
		if(clear_flag){
			clear_flag = false;
			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 a = Double.parseDouble(s1);
			double b = Double.parseDouble(s2);
			
			if(op.equals("+")){
				result = a+b;
			}
			else if(op.equals("-")){
				result = a-b;
			}
			else if(op.equals("×")){
				result = a*b;
			}
			else if(op.equals("÷")){
				if(b == 0)
					result = 0;
				else
					result = a/b;
			}
			if(!s1.contains(".") && !s2.contains(".") && !op.equals("÷")){
				int r = (int)result;
				edt1.setText(r+"");
				edt1.setSelection(edt1.length());
			}
			else{
				edt1.setText(result+"");
				edt1.setSelection(edt1.length());
			}
		}
		else if(!s1.equals("") && s2.equals("")){
			edt1.setText(exp);
			edt1.setSelection(edt1.length());
		}
		else if(s1.equals("") && !s2.equals("")){
			double b = Double.parseDouble(s2);
			if(op.equals("+")){
				result = b;
			}
			else if(op.equals("-")){
				result = 0-b;
			}
			else if(op.equals("×")){
				result = 0;
			}
			else if(op.equals("÷")){
				result = 0;
			}
			if(!s2.contains(".")){
				int r = (int)result;
				edt1.setText(r+"");
				edt1.setSelection(edt1.length());
			}
			else{
				edt1.setText(result+"");
				edt1.setSelection(edt1.length());
			}
		}
		else{
			edt1.setText("");
			edt1.setSelection(edt1.length());
		}
		
	}
	public boolean judge(){
		String exp = edt1.getText().toString();
		int num = 0;
		for(int i=0;i<exp.length();i++){
			if(exp.charAt(i)==' ')
				num++;
		}
		if(num>2){
			return false;
		}
		else{
			return true;
		}
	}


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值