Android简单计算器

jopen 10年前

android简单计算器制作起来来其实很简单,如果要做复杂的就得花大把的时间啦!

下面是效果图:

20131214101107234.jpg

只需要编写一个MainActivity类和一个XML文件布局就可以啦!


布局Xml文件如下:

    <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="wrap_content"            android:orientation="vertical" >             <!-- 显示框 -->                    <TextView                android:id="@+id/textView"                android:layout_width="fill_parent"                android:layout_height="60dip"                android:layout_marginTop="4sp"                android:layout_marginBottom="10sp"                android:layout_marginLeft="8sp"                android:layout_marginRight="8sp"                android:background="@drawable/bgselector"                                android:text=""                android:hint="0."                android:singleLine="true"                android:textSize="@dimen/activity_vertical_TextSize"                 android:gravity="right"/>                      <Button android:id="@+id/clear"                    android:layout_width="fill_parent"                    android:layout_height="wrap_content"                     android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="c"/>                        <!-- 中间按钮 -->             <TableLayout                  android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:stretchColumns="0,1,2,3"                                >                <TableRow >                <Button android:id="@+id/btn7"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="7"/>                <Button android:id="@+id/btn8"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="8"/>                   <Button android:id="@+id/btn9"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="9"/>                  <Button android:id="@+id/add"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="+"/>                 </TableRow>                   <TableRow >                <Button android:id="@+id/btn4"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="4"/>                   <Button android:id="@+id/btn5"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="5"/>                <Button android:id="@+id/btn6"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="6"/>                 <Button android:id="@+id/cut"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="-"/>                </TableRow>                 <TableRow >                <Button android:id="@+id/btn1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="1"/>                   <Button android:id="@+id/btn2"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="2"/>                <Button android:id="@+id/btn3"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="3"/>                 <Button android:id="@+id/rid"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="*"/>                </TableRow>                                <TableRow >                <Button android:id="@+id/btn0"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="0"/>                   <Button android:id="@+id/btnPoint"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="."/>                <Button android:id="@+id/btnResult"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="="/>                  <Button android:id="@+id/divide"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:background="@drawable/btnselector"                    android:layout_margin="@dimen/activity_vertical_margin2"                    android:textSize="@dimen/activity_vertical_TextSize"                    android:text="/"/>                </TableRow>                                                                                                                                                                     </TableLayout>                               </LinearLayout>  
MainActivity类代码如下:
public class MainActivity extends Activity {        TextView textView;         Button button1;         Button button2;         Button button3;         Button button4;         Button button5;         Button button6;         Button button7;         Button button8;         Button button9;         Button button0;         Button add;//加         Button cut;//减         Button rid;//乘         Button divide;//除         Button result;         Button point;         Button clear;         int pointCount=0;         int option = 0;//运算符状态         boolean newdigital=true;//标记是否是新输入的数字         boolean flag=true;//判断程序是否出错         double a=0,b=0;//两个相加的数         double sum=0;         double sumtype=0;//判断输出的数是否有小数部分        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            requestWindowFeature(Window.FEATURE_NO_TITLE);            setContentView(R.layout.activity_main);             textView=(TextView)findViewById(R.id.textView);             button1=(Button)findViewById(R.id.btn1);             button2=(Button)findViewById(R.id.btn2);             button3=(Button)findViewById(R.id.btn3);             button4=(Button)findViewById(R.id.btn4);             button5=(Button)findViewById(R.id.btn5);             button6=(Button)findViewById(R.id.btn6);             button7=(Button)findViewById(R.id.btn7);             button8=(Button)findViewById(R.id.btn8);             button9=(Button)findViewById(R.id.btn9);             button0=(Button)findViewById(R.id.btn0);             add=(Button)findViewById(R.id.add);             cut=(Button)findViewById(R.id.cut);             rid=(Button)findViewById(R.id.rid);             divide=(Button)findViewById(R.id.divide);             result=(Button)findViewById(R.id.btnResult);             point=(Button)findViewById(R.id.btnPoint);             clear=(Button)findViewById(R.id.clear);                         button0.setOnClickListener(lisenter);             button1.setOnClickListener(lisenter);             button2.setOnClickListener(lisenter);             button3.setOnClickListener(lisenter);             button4.setOnClickListener(lisenter);             button5.setOnClickListener(lisenter);             button6.setOnClickListener(lisenter);             button7.setOnClickListener(lisenter);             button8.setOnClickListener(lisenter);             button9.setOnClickListener(lisenter);             add.setOnClickListener(lisenter);             cut.setOnClickListener(lisenter);             rid.setOnClickListener(lisenter);             divide.setOnClickListener(lisenter);             result.setOnClickListener(lisenter);             point.setOnClickListener(lisenter);             clear.setOnClickListener(lisenter);                                              }         OnClickListener lisenter=new OnClickListener() {                                @Override                public void onClick(View v) {                    TextView text = (TextView) findViewById(R.id.textView);                    String s = text.getText().toString();//获取文本框显示的字符串                    Button btn =(Button)v;                    String t=(String) btn.getText();//获取按钮的字符                    //数字的输入                    if(btn.getId()==R.id.btn0||btn.getId()==R.id.btn1||btn.getId()==R.id.btn2||btn.getId()==R.id.btn3                        ||btn.getId()==R.id.btn4||btn.getId()==R.id.btn5||btn.getId()==R.id.btn6||                        btn.getId()==R.id.btn7||btn.getId()==R.id.btn8||btn.getId()==R.id.btn9||(btn.getId()==R.id.btnPoint&&pointCount==0))                    {                                                //Toast.makeText(MainActivity.this, btn.getText(), Toast.LENGTH_LONG).show();                        //Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();                        if(btn.getId()==R.id.btnPoint){                            if(null==s||s.equals("")){                                s+="0"+btn.getText();                            }else{                                s+=btn.getText();                                                   }                            pointCount=1;                        }else{                            s+=btn.getText();                        }                        text.setText(s);                                            }                    //运算符的输入                    if(btn.getId()==R.id.add||btn.getId()==R.id.divide||btn.getId()==R.id.cut||btn.getId()==R.id.rid){                        //Toast.makeText(MainActivity.this, btn.getText(), Toast.LENGTH_LONG).show();                        //如果已经有两个数,再按运算符就直接把结果运算出来保存到a中然后继续运算                        if(null==s||s.equals("")){                            s="0";                        }                        if(option!=0){                            b=Double.valueOf(s);                            switch (option) {                                case 1:                                    sum=a+b;                                    break;                                case 2:                                    sum=a-b;                                    break;                                case 3:                                    sum=a*b;                                    break;                                case 4:                                    if(b==0){                                        Toast.makeText(MainActivity.this, "0不能为除数", Toast.LENGTH_LONG).show();                                        text.setText("");                                        break;                                    }                                    sum=a/b;                                    break;                                default:                                    break;                                }                                a=sum;                                                            }                        if(option==0){                            a=Double.valueOf(s);                        }                        switch (btn.getId()) {                            case R.id.add:                                option=1;                                break;                            case R.id.cut:                                option=2;                                break;                            case R.id.rid:                                option=3;                                break;                            case R.id.divide:                                option=4;                                break;                            default:                                break;                        }                                                text.setText("");                                                                       }                    //等于,运算结果                    if(btn.getId()==R.id.btnResult){                        //Toast.makeText(MainActivity.this, btn.getText(), Toast.LENGTH_LONG).show();                        if(null==s||s.equals("")){                            s="0";                        }                        b=Double.valueOf(s);                        switch (option) {                            case 1:                                sum=a+b;                                break;                            case 2:                                sum=a-b;                                break;                            case 3:                                sum=a*b;                                break;                            case 4:                                if(b==0){                                    Toast.makeText(MainActivity.this, "0不能为除数", Toast.LENGTH_LONG).show();                                    text.setText("");                                    flag=false;                                    break;                                }                                sum=a/b;                                break;                            default:                                break;                        }                                                sumtype=sum%1;                        if(sumtype>0){                            pointCount=1;                        }                        s=""+sum;                        if(sumtype==0){                            int end=(s.toString()).lastIndexOf(".");                            String str=(s.toString()).substring(0, end);                                                    s=""+Integer.parseInt(str);                            pointCount=0;                        }                        if(flag){                            text.setText(s);                        }                        a=Double.valueOf(s);                        option=0;                        flag=true;                                            }                    //清除                    if(btn.getId()==R.id.clear){                        //Toast.makeText(MainActivity.this, btn.getText(), Toast.LENGTH_LONG).show();                        text.setText("");                        pointCount=0;                        option=0;                        flag=true;                    }                }            };                        }