关闭

android caculator

375人阅读 评论(0) 收藏 举报

还只是个架子,没实现算术计算

mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/txtMsg"
            android:inputType="number"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="">
        </EditText>    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
     	<Button
     		android:id="@+id/btnmc"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="mc" android:layout_weight="1">
     	</Button>
     	<Button
     		android:id="@+id/btnmp"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="m+" android:layout_weight="1">
     	</Button>
     	<Button
     		android:id="@+id/btnmm"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="m-" android:layout_weight="1">
     	</Button>
     	<Button
     		android:id="@+id/btnmr"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="mr" android:layout_weight="1">
     	</Button>
  </LinearLayout>
    
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" >
          <Button
     		android:id="@+id/btnClear"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="C" >
          </Button>
          <Button
     		android:id="@+id/btnpOrm"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="+/-" >
          </Button>
          <Button
     		android:id="@+id/btnDevide"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="/" >
          </Button>
          <Button
     		android:id="@+id/btnAnd"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="*" >
          </Button>
      </LinearLayout>
    
      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
     	<Button
     		android:id="@+id/btnSeven"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="7" android:layout_weight="1">
     	</Button>
     	<Button     	
     		android:id="@+id/btnEight"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="8" android:layout_weight="1">
     	</Button>
     	<Button
     		android:id="@+id/btnNine"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="9" android:layout_weight="1">
     	</Button>
     	<Button
     		android:id="@+id/btnMinus"
     	    android:layout_width="fill_parent"
     	    android:layout_height="wrap_content"
     	    android:text="-" android:layout_weight="1">
     	</Button>
  </LinearLayout>
  
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
     		android:id="@+id/btnFour"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" >
        </Button>
        <Button
     		android:id="@+id/btnFive"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" >
        </Button>
        <Button
     		android:id="@+id/btnSix"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" >
        </Button>
        <Button
     		android:id="@+id/btnPlus"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" >
        </Button>
    </LinearLayout>
    
     <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:orientation="vertical"
        	android:layout_weight="3"
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content">

     		<LinearLayout android:orientation="horizontal"
        		android:layout_width="fill_parent"
        		android:layout_height="wrap_content">
        		<Button
     		android:id="@+id/btnOne"
        		    android:layout_width="wrap_content"
        		    android:layout_height="wrap_content"
        		    android:layout_weight="1"
        		    android:text="1"></Button>
        		<Button
     		android:id="@+id/btnTwo"
        		    android:layout_width="wrap_content"
        		    android:layout_height="wrap_content"
        		    android:layout_weight="1"
        		    android:text="2"></Button>
        		<Button
     		android:id="@+id/btnThree"
        		    android:layout_width="wrap_content"
        		    android:layout_height="wrap_content"
        		    android:layout_weight="1"
        		    android:text="3"></Button>
     		</LinearLayout>
      
     		<LinearLayout android:orientation="horizontal"
        		android:layout_width="fill_parent"
        		android:layout_height="wrap_content">
        		<Button
     		android:id="@+id/btnZero"
        		    android:layout_width="0px"
        		    android:layout_height="wrap_content"
        		    android:layout_weight="2"
        		    android:text="0"></Button>
        		<Button
     		android:id="@+id/btnDot"
        		    android:layout_width="0px"
        		    android:layout_height="wrap_content"
        		    android:layout_weight="1"
        		    android:text="."></Button>
        	</LinearLayout>	
     	</LinearLayout>
    
    <LinearLayout android:orientation="vertical"
        android:layout_weight="1"
        	android:layout_width="wrap_content"
        	android:layout_height="fill_parent">
     		<Button
     		android:id="@+id/btnEqual"
     		    android:layout_width="fill_parent"
     		    android:layout_height="fill_parent"
        android:text="="></Button>
      </LinearLayout>
     </LinearLayout>   

</LinearLayout>



MyActivity.java


package test.my;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
			setClickListener();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void setClickListener() throws IllegalArgumentException, IllegalAccessException{
        Field[] fields = R.id.class.getDeclaredFields();
        
		for(Field f: fields){
			View v = findViewById(f.getInt(R.id.class));
			
			if(f.getName().startsWith("btn")){
	        	v.setOnClickListener(new MyButtonClickListener());
	        	
			} else if(f.getName().startsWith("txt")){
				((EditText)v).setInputType(InputType.TYPE_NULL);
				
			}
		}

    }
    
    public class MyButtonClickListener implements OnClickListener{
    	public void onClick(View v){    		
    		Button btn = (Button)v;
    		EditText et = (EditText)findViewById(R.id.txtMsg);

    		et.setText(btn.getText());
    		switch(btn.getId()){
				case R.id.btnClear:
					et.setText("");
					break;
					
				default:
					et.setText(btn.getText());
					break;
    		}
    				
    	}
    }
}



R.java


/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package test.my;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int btnAnd=0x7f050008;
        public static final int btnClear=0x7f050005;
        public static final int btnDevide=0x7f050007;
        public static final int btnDot=0x7f050015;
        public static final int btnEight=0x7f05000a;
        public static final int btnEqual=0x7f050016;
        public static final int btnFive=0x7f05000e;
        public static final int btnFour=0x7f05000d;
        public static final int btnMinus=0x7f05000c;
        public static final int btnNine=0x7f05000b;
        public static final int btnOne=0x7f050011;
        public static final int btnPlus=0x7f050010;
        public static final int btnSeven=0x7f050009;
        public static final int btnSix=0x7f05000f;
        public static final int btnThree=0x7f050013;
        public static final int btnTwo=0x7f050012;
        public static final int btnZero=0x7f050014;
        public static final int btnmc=0x7f050001;
        public static final int btnmm=0x7f050003;
        public static final int btnmp=0x7f050002;
        public static final int btnmr=0x7f050004;
        public static final int btnpOrm=0x7f050006;
        public static final int txtMsg=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
        public static final int hello=0x7f040001;
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49122次
    • 积分:916
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:25篇
    • 译文:0篇
    • 评论:11条
    最新评论