android应用开发实践二----控件学习的计算器实践

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >

   <EditText
       android:id="@+id/text"
       android:layout_width="fill_parent"
       android:layout_height="170dip"
       android:background="@drawable/hint"
       android:inputType="number"
       android:maxLines="4"
       android:textSize="60sp"
       android:gravity="center"
       android:paddingLeft="18dp"
       android:paddingRight="24dp"
       android:textColor="#00ff7f" />

    <Button
      android:id="@+id/MyButon0"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/text" 
      android:textColor="#FF0000"
      android:textSize="60sp"
      android:text="@string/zero"
    />
      <Button
      android:id="@+id/MyButon1"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/text" 
      android:layout_toRightOf="@id/MyButon0"
      android:textColor="#FF0000"
      android:text="@string/one"
      android:textSize="60sp"
           />
          <Button
      android:id="@+id/MyButon2"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/text" 
      android:layout_toRightOf="@id/MyButon1"
      android:textColor="#FF0000"
      android:text="@string/two"
      android:textSize="60sp"
           />
       <Button
      android:id="@+id/MyButon3"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/text" 
      android:layout_toRightOf="@id/MyButon2"
      android:textColor="#FF0000"
      android:text="@string/three"
      android:textSize="60sp"
           />
            <Button
      android:id="@+id/MyButon4"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/text" 
      android:layout_toRightOf="@id/MyButon3"
      android:textColor="#FF0000"
      android:text="@string/four"
      android:textSize="60sp"
           />
              <Button
      android:id="@+id/MyButon5"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/MyButon0" 
      android:textColor="#FF0000"
      android:text="@string/five"
      android:textSize="60sp"
           />
               <Button
      android:id="@+id/MyButon6"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/MyButon5"
      android:layout_below="@id/MyButon1" 
      android:textColor="#FF0000"
      android:text="@string/six"
      android:textSize="60sp"
           />
          <Button
      android:id="@+id/MyButon7"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/MyButon6"
      android:layout_below="@id/MyButon2" 
      android:textColor="#FF0000"
      android:text="@string/seven"
      android:textSize="60sp"
           />
       <Button
      android:id="@+id/MyButon8"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/MyButon7"
      android:layout_below="@id/MyButon3" 
      android:textColor="#FF0000"
      android:text="@string/eight"
      android:textSize="60sp"
           />
           <Button
      android:id="@+id/MyButon9"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/MyButon8"
      android:layout_below="@id/MyButon3" 
      android:textColor="#FF0000"
      android:text="@string/nine"
      android:textSize="60sp"
           />
                  <Button
      android:id="@+id/ADD"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_below="@id/MyButon5" 
      android:textColor="#FF0000"
      android:text="@string/add"
      android:textSize="60sp"
           />
               <Button
      android:id="@+id/SUB"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/ADD"
      android:layout_below="@id/MyButon6" 
      android:textColor="#FF0000"
      android:text="@string/sub"
      android:textSize="60sp"
           />
           <Button
      android:id="@+id/MULTI"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/SUB"
      android:layout_below="@id/MyButon7" 
      android:textColor="#FF0000"
      android:text="@string/multi"
      android:textSize="60sp"
           />
                 <Button
      android:id="@+id/DIV"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/MULTI"
      android:layout_below="@id/MyButon8" 
      android:textColor="#FF0000"
      android:text="@string/div"
      android:textSize="60sp"
           />
                         <Button
      android:id="@+id/EQUE"
      android:layout_width="71dip"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/DIV"
      android:layout_below="@id/MyButon9" 
      android:textColor="#FF0000"
      android:text="@string/eque"
      android:textSize="60sp"
           />
     
       <Button
         android:id="@+id/opposite"
         android:layout_width="71dip"
         android:layout_height="90dip"
         android:layout_below="@id/ADD" 
         android:layout_alignParentLeft="true"
         android:text="@string/opposite"
         android:textColor="#FF0000"
         android:textSize="32sp" />
         <Button
         android:id="@+id/dot"
         android:layout_width="71dip"
         android:layout_height="90dip"
         android:layout_below="@id/SUB" 
          android:layout_toRightOf="@id/opposite"
         android:text="@string/dot"
         android:textColor="#FF0000"
         android:textSize="30sp" 
         android:gravity="center"
         />
     <Button
         android:id="@+id/CRL"
          android:layout_width="71dip"
         android:layout_height="90dip"
         android:layout_below="@id/MULTI" 
          android:layout_toRightOf="@id/dot"
         android:text="@string/clear"
         android:textColor="#0000FF"
         android:textSize="30sp" />

                <Button
                    android:id="@+id/OFF"
                     android:layout_width="71dip"
                     android:layout_height="90dip"
                    android:layout_below="@id/DIV" 
                    android:layout_toRightOf="@id/CRL"
                    android:text="@string/off"
                    android:textColor="#000000"
                    android:textSize="25sp" />

                <Button
                    android:id="@+id/ON"
                    android:layout_width="71dip"
                    android:layout_height="90dip"
                    android:layout_below="@id/EQUE" 
                    android:layout_toRightOf="@id/OFF"
                    android:text="@string/on"
                    android:textColor="#7fffd4"
                    android:textSize="30sp" />

              
                   
</RelativeLayout >

启动文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calculator"
    android:versionCode="1"
    android:versionName="1.0" >
     
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <supports-screens
        android:smallScreens="true" 
        android:normalScreens="true" 
        android:largeScreens="true" 
        android:anyDensity="true"
    />
      <application
        android:allowBackup="true"
        android:icon="@drawable/calculate_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.calculator.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden"
            >
                    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

资源文件string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="log">Hfl stduio for test\t</string>
    <string name="app_name">Calculator</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="tips">please input nuber:</string>
    <string name="opposite">+/-</string>
    <string name="dot">.</string>
    <string name="zero">0</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="four">4</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="add">+</string>
    <string name="sub">-</string>
    <string name="multi">*</string>
    <string name="div">/</string>
    <string name="eque">=</string>
    <string name="clear">C</string>
    <string name="on">ON</string>
     <string name="off">OFF</string>
</resources>

java源代码

package com.example.calculator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.*;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
	 EditText edit;
	 private int option = 0;//运算符状态
	 private boolean newdigital=false;//标记是否是新输入的数字
	 private double a=0,b=0;//两个相加的数
	 private boolean flag=false;
	 private int First=0;
	 private int dot=0;

	 
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    edit=(EditText)findViewById(R.id.text); 
	    edit.setInputType(InputType.TYPE_NULL);
	    Button bt0=(Button)findViewById(R.id.MyButon0);
		Button bt1=(Button)findViewById(R.id.MyButon1);
		Button bt2=(Button)findViewById(R.id.MyButon2);
		Button bt3=(Button)findViewById(R.id.MyButon3);
	    Button bt4=(Button)findViewById(R.id.MyButon4);
		Button bt5=(Button)findViewById(R.id.MyButon5);
		Button bt6=(Button)findViewById(R.id.MyButon6);
		Button bt7=(Button)findViewById(R.id.MyButon7);
	    Button bt8=(Button)findViewById(R.id.MyButon8);
		Button bt9=(Button)findViewById(R.id.MyButon9);
		Button add=(Button)findViewById(R.id.ADD);
		Button sub=(Button)findViewById(R.id.SUB);
		Button multi=(Button)findViewById(R.id.MULTI);
		Button opp=(Button)findViewById(R.id.opposite);
		Button dot=(Button)findViewById(R.id.dot);
		Button div=(Button)findViewById(R.id.DIV);
		Button eque=(Button)findViewById(R.id.EQUE);
		Button crl=(Button)findViewById(R.id.CRL);
		Button on=(Button)findViewById(R.id.ON);
		Button off=(Button)findViewById(R.id.OFF);
	    bt0.setOnClickListener(listener);
	    bt1.setOnClickListener(listener);
	    bt2.setOnClickListener(listener);
	    bt3.setOnClickListener(listener);
	    bt4.setOnClickListener(listener);
	    bt5.setOnClickListener(listener);
	    bt6.setOnClickListener(listener);
	    bt7.setOnClickListener(listener);
	    bt8.setOnClickListener(listener);
	    bt9.setOnClickListener(listener);
	    add.setOnClickListener(listener);
	    sub.setOnClickListener(listener);
	    multi.setOnClickListener(listener);
	    div.setOnClickListener(listener);
	    eque.setOnClickListener(listener);
	    crl.setOnClickListener(listener);
	    on.setOnClickListener(listener);
	    off.setOnClickListener(listener);
	    opp.setOnClickListener(listener);
	    dot.setOnClickListener(listener);
	}
    private OnClickListener listener =new OnClickListener()
	{
    	
		@Override
		public void onClick(View v) {
			String s;
			
			s = edit.getText().toString();
			if((s.equals("0")==true))
			{
				newdigital=true;
			   
			}

			if(First>8)
			{
				edit.setText("FFFFFFFFF");
				newdigital=true;
				First=0;
				dot=0;
				flag=false;
				return;
			}
		
			Button btx=(Button)v;
		
			
			switch(btx.getId())
			{
				
				case R.id.ON:
					
					edit.setText("0");
					First=0;
					flag=false;
					dot=0;
					
					break;
				case R.id.OFF:
					edit.setText("FFFFFFFFF");
					First=0;
					flag=false;
					dot=0;
					newdigital=true;
					break;
				case R.id.CRL:
					edit.setText("0"); 
					First=0;
					dot=0;
					flag=false;
					break;
				case R.id.opposite:
					if(s.length()==0)
					{
						a=0;
						b=0;
						option=0;
						newdigital=true;
						edit.setText("");
						
						flag=false;
						dot=0;
						break;
					}
					else if(s!="")
						{
						a=Double.parseDouble(s);
						edit.setText(String.valueOf(-a));
						newdigital=true;
						
						flag=false;
						dot=0;
					 	break;
						}
				case R.id.dot:
					
					if((s.equals("0")==true&&newdigital)||(First==0))
					{
						flag=true;
					}
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					if(dot==0)
					{
					   dot++;
					   if(flag)
						   edit.setText("0.");
					    else
						   edit.setText(s+".");
										   
					}
					First++;
					
					break;
				
				case R.id.MyButon0:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"0"); 
					First++;
					break;
				case R.id.MyButon1:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"1"); 	
					First++;
					break;
				case R.id.MyButon2:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"2"); 
					First++;
					break;
				case R.id.MyButon3:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"3"); 	
					First++;
					break;
				case R.id.MyButon4:
					if(newdigital)
					{
						s="";
						newdigital=false;
						
					}
					edit.setText(s+"4"); 	
					First++;
					break;
				case R.id.MyButon5:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"5"); 	
					First++;
					break;
				case R.id.MyButon6:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"6"); 	
					First++;
					break;
				case R.id.MyButon7:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"7"); 	
					First++;
					break;
				case R.id.MyButon8:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"8"); 	
					First++;
					break;
				case R.id.MyButon9:
					if(newdigital)
					{
						s="";
						newdigital=false;
					}
					edit.setText(s+"9"); 	
					First++;
					break;
				case R.id.ADD:
				  
				   if(s.equals("")==true||s.equals("FFFFFFFFF")==true)
				   {
					   s="0";
				   }
					a=Double.parseDouble(s);
					s="";
					option=1;
					edit.setText(""); 
					flag=false;
					First=0;
					dot=0;
				
					
					break;
				case R.id.SUB:
					 if(s.equals("")==true||s.equals("FFFFFFFFF")==true)
					   {
						   s="0";
					   }
					a=Double.parseDouble(s);
					s="";
					option=2;
					edit.setText(""); 
					First=0;
					flag=false;
				
					dot=0;
				    break;
				case R.id.MULTI:
					 if(s.equals("")==true||s.equals("FFFFFFFFF")==true)
					   {
						   s="0";
					   }
					a=Double.parseDouble(s);
					s="";
					option=3;
					edit.setText("");  
					First=0;
				
					flag=false;
					dot=0;
					break;
				case R.id.DIV:
					 if(s.equals("")==true||s.equals("FFFFFFFFF")==true)
					   {
						   s="0";
					   }
					a=Double.parseDouble(s);
					dot=0;
					option=4;
					
					edit.setText(""); 
					First=0;
					flag=false;
					break;
				case R.id.EQUE:
				{
					switch(option)
					{
					  case 1:
						  b=Double.parseDouble(s);
						  edit.setText(String.valueOf(a+b));
						 break;
					 case 2:
						 b=Double.parseDouble(s);
						 edit.setText(String.valueOf(a-b));
						 break;
					 case 3:
						 b=Double.parseDouble(s);
						 edit.setText(String.valueOf(a*b));
						 break;
					 case 4:
					       {
									 b=Double.parseDouble(s);
									 if(b!=0)
									 {
									 edit.setText(String.valueOf(a/b));
									 break;
									 }
									 else
									 {
										 edit.setText("FFFFFFFFF");
										 break;						
									}
						    }
				     			      
					      
					   }
					s="";
					First=0;
					flag=false;
					dot=0;
					newdigital=true;
					break;
			    	}
					
				}
					
							
			
		   

		
}
};
}

最后结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江海细流

如该文章对你有帮助,请支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值