以前自己写过java版的简单计算器 代码其实都一样就是改改布局
不过我现在不会获取屏幕大小。。然后按照比例设置按钮的长宽。所以先这样凑合把
都在左上角。。
/*知道怎么获取屏幕的尺寸了
int height=this.getResources().getDisplayMetrics().heightPixels;//获取屏幕的高
int width=this.getResources().getDisplayMetrics().widthPixels;//获取屏幕的宽
然后在MainActivity.java中的onCreate方法里面for循环里面添加
button[i].setWidth(width/4);
button[i].setHeight(height/6);
就能让计算器按钮铺满屏幕了~我就不修改了 */
我用的相对布局 感觉这样比较方便
layout文件
<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:id="@+id/relativeLayout" > <EditText android:id="@+id/print" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(" android:gravity="center" android:layout_below="@+id/print" /> <Button android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=")" android:gravity="center" android:layout_below="@+id/print" android:layout_toRightOf="@+id/left" /> <Button android:id="@+id/delect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除" android:gravity="center" android:layout_below="@+id/print" android:layout_toRightOf="@+id/right" /> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退格" android:gravity="center" android:layout_below="@+id/print" android:layout_toRightOf="@+id/delect" /> <Button android:id="@+id/seven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" android:gravity="center" android:layout_below="@+id/left" /> <Button android:id="@+id/eight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" android:gravity="center" android:layout_below="@+id/right" android:layout_toRightOf="@+id/seven" /> <Button android:id="@+id/nine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:gravity="center" android:layout_below="@+id/delect" android:layout_toRightOf="@+id/eight" /> <Button android:id="@+id/div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:gravity="center" android:layout_below="@+id/back" android:layout_toRightOf="@+id/delect" /> <Button android:id="@+id/four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:gravity="center" android:layout_below="@+id/seven" /> <Button android:id="@+id/five" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:gravity="center" android:layout_below="@+id/eight" android:layout_toRightOf="@+id/four" /> <Button android:id="@+id/six" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" android:gravity="center" android:layout_below="@+id/nine" android:layout_toRightOf="@+id/five" /> <Button android:id="@+id/mul" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:gravity="center" android:layout_below="@+id/div" android:layout_toRightOf="@+id/nine" /> <Button android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:gravity="center" android:layout_below="@+id/four" /> <Button android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:gravity="center" android:layout_below="@+id/five" android:layout_toRightOf="@+id/one" /> <Button android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:gravity="center" android:layout_below="@+id/six" android:layout_toRightOf="@+id/two" /> <Button android:id="@+id/sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:gravity="center" android:layout_below="@+id/mul" android:layout_toRightOf="@+id/three" /> <Button android:id="@+id/point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." android:gravity="center" android:layout_below="@+id/one" /> <Button android:id="@+id/zero" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:gravity="center" android:layout_below="@+id/two" android:layout_toRightOf="@+id/point" /> <Button android:id="@+id/equal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:gravity="center" android:layout_toRightOf="@+id/zero" android:layout_below="@+id/three" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:gravity="center" android:layout_toRightOf="@+id/equal" android:layout_below="@+id/sub" /> </RelativeLayout>
package com.example.simplecalculator; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { //private Button one,two,three,four,five,six,seven,eight,nine,ten,point,add,div,mul,sub; private Button button[]; public EditText text; private int id[]={R.id.one,R.id.two,R.id.three,R.id.four,R.id.five,R.id.six,R.id.seven, R.id.eight,R.id.nine,R.id.zero,R.id.point,R.id.add,R.id.sub,R.id.mul,R.id.div, R.id.left,R.id.right,R.id.delect,R.id.back,R.id.equal}; private EditText editText; private Cal cal_str; boolean equal=false; public MainActivity() { // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button =new Button[id.length]; for(int i=0;i<id.length;i++){ button[i]=(Button) findViewById(id[i]); button[i].setOnClickListener(MainActivity.this); } } @Override public void onClick(View view) { // TODO Auto-generated method stub Button btn =(Button) findViewById(view.getId()); EditText print=(EditText) findViewById(R.id.print); if(btn.getId()==R.id.delect){ print.setText(null); } else if(btn.getId()==R.id.back){ char str[]=print.getText().toString().toCharArray(); if(str.length>=1) print.setText(new String(str,0,str.length-1)); } else if(btn.getId()==R.id.equal){ String str=print.getText().toString(); if(str.equals("1+99")||str.equals("99+1")){ Toast.makeText(MainActivity.this, "我向你走了99步,最后一步我们一起走。", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "I LOVE YOU", Toast.LENGTH_LONG).show(); } equal=true; cal_str=new Cal(print,str+"="); } else{ if(equal){ equal=false; print.setText(null); } print.setText(print.getText()+btn.getText().toString()); } } }
package com.example.simplecalculator; import java.util.Stack; import android.widget.EditText; import android.widget.Toast; public class Cal { public Cal(EditText print,String str) { // TODO Auto-generated constructor stub char temp[]=new char[100]; Stack<Character> optr=new Stack<Character>(); Stack<Double> opnd=new Stack<Double>(); int k=0,count=0; boolean flag=false; optr.clear(); opnd.clear(); optr.add('='); try{ for(int i=0;i<str.length();){ if(count++>1000) flag=true; char ch=str.charAt(i); if(Character.isDigit(ch)||ch=='.'){ temp[k++]=ch; i++; continue; } if(k!=0){ opnd.add(Double.parseDouble(new String(temp,0,k))); k=0; } switch(compare(optr.peek(),ch)){ case -1:flag=true;break; case 60:optr.add(ch);i++;break; case 61:optr.pop();i++;break; case 62:opnd.add(result(opnd.pop(),opnd.pop(),optr.pop()));break; } if(flag){ break; } } if(flag) print.setText("请输入合法字符"); else print.setText(opnd.pop().toString()); } catch(Exception e){ print.setText("请输入合法字符"); } } private Double result(Double num1, Double num2, char ch) { // TODO Auto-generated method stub if(ch=='+') return num2+num1; if(ch=='-') return num2-num1; if(ch=='*') return num2*num1; if(ch=='/') return num2/num1; return num2; } private int compare(char ch1, char ch2) { // TODO Auto-generated method stub String oper[]={">><<<>>",">><<<>>",">>>><>>", ">>>><>>","<<<<<= ",">>>> >>","<<<<< ="}; int x,y; x=to_num(ch1); y=to_num(ch2); if(x==-1||y==-1) return -1; return (int)(oper[x].charAt(y)); } private int to_num(char ch) { // TODO Auto-generated method stub if(ch=='+') return 0; else if(ch=='-') return 1; else if(ch=='*') return 2; else if(ch=='/') return 3; else if(ch=='(') return 4; else if(ch==')') return 5; else if(ch=='=') return 6; else return -1; } }
附上我的java简单计算器地址把点击打开链接