android实现简单的计算器

计算器好像是每个程序猿练安卓的入门应用,那么我也试试
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:editable="false" 
        android:gravity="right|bottom"
    android:background="@drawable/white_bg"
  
   />
<TableLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:paddingBottom="@dimen/activity_vertical_margin"
             android:paddingLeft="@dimen/activity_horizontal_margin"
             android:paddingRight="@dimen/activity_horizontal_margin"
             android:paddingTop="@dimen/activity_vertical_margin"
             android:stretchColumns="*"
             tools:context=".TableLayoutActivityFragment"
             tools:showIn="@layout/activity_table_layout"     
            >
            
    <TableRow
       
>
        <Button
           
            android:background="@drawable/white_se"
            android:layout_marginTop="20dp"
            android:layout_height="60dp"
            android:id="@+id/btn_C"
            android:text="C"
            android:textSize="40dp"/>
        <Button
            android:background="@drawable/white_se"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_height="60dp"
            android:id="@+id/btn_DEL"
            android:text="DEL"
            android:textSize="40dp"/>
        <Button
            android:background="@drawable/white_se"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_height="60dp"
            android:id="@+id/btn_sin"
            android:text="sin"
            android:textSize="40dp"/>
        <Button
            android:background="@drawable/white_se"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_height="60dp"
            android:id="@+id/btn_genhao"
            android:text="√"
            android:textSize="40dp"/>
    </TableRow>
    <TableRow>
        <Button
            android:background="@drawable/white_se"
            android:layout_marginTop="20dp"
            android:layout_height="60dp"
            android:id="@+id/btn_7"
            android:text="7"
            android:textSize="40dp"/>
        <Button
            android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_8"
            android:text="8"
               android:textSize="40dp"/>
        <Button        android:background="@drawable/white_se"
                android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_9"
            android:text="9"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_chu"
            android:text="÷"
               android:textSize="40dp"/>
    </TableRow>
    <TableRow>
        <Button
                    android:background="@drawable/white_se"
                        android:layout_marginTop="20dp"
                               android:layout_height="60dp"
             android:id="@+id/btn_4"
            android:text="4"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_5"
            android:text="5"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                 android:layout_height="60dp"
                                android:layout_marginLeft="10dp"
             android:id="@+id/btn_6"
            android:text="6"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_cheng"
            android:text="×"
               android:textSize="40dp"/>
    </TableRow>
    <TableRow>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                 android:layout_height="60dp"
             android:id="@+id/btn_1"
            android:text="1"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_2"
            android:text="2"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_3"
            android:text="3"
               android:textSize="40dp"/>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
             android:id="@+id/btn_jian"
            android:text="-"
               android:textSize="40dp"/>
    </TableRow>
    <TableRow>
        <!-- 嵌套一个TableLayout 占用3个单元格宽度,所有列可收缩-->
        <TableLayout
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:stretchColumns="*"
            >
            <!-- 一行设置3个没有高度的控件,让父TableLayout知道此表格有3列
            这样下面一行才可以设置android:layout_span="2" 合并2个单元格-->
            <TableRow>
                <Button
                   
                    android:layout_height="0dp"
                    />
                <Button
                    android:layout_height="0dp"
                    />
                <Button
                    android:layout_height="0dp"
                    />

            </TableRow>
            <TableRow>
                <!-- 占用2个单元格-->
                <Button
                    android:id="@+id/btn_0"
                    android:layout_marginTop="20dp"
                           android:layout_height="60dp"
                    android:layout_span="2"
                    android:background="@drawable/white_se"
                    android:text="0"
                       android:textSize="40dp"/>
                <Button
                              android:background="@drawable/white_se"
                                  android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                       android:layout_height="60dp"
                     android:id="@+id/btn_dian"
                    android:text="."
                       android:textSize="40dp"/>

            </TableRow>

            <!-- 可以设置单元格高度,不可以设置其宽度
            单独一个控件 占满一整行宽度-->
            <Button
                          android:background="@drawable/white_se"
                               android:layout_marginTop="20dp"
                                
                 android:id="@+id/btn_deng"
                android:layout_height="100dp"
                android:text="="
                   android:textSize="40dp"/>

        </TableLayout>
        <Button
                      android:background="@drawable/white_se"
                          android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                              
             android:id="@+id/btn_jia"
            android:layout_height="match_parent"
            android:text="+"
               android:textSize="40dp"/>
    </TableRow>

</TableLayout>
</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<resources>
   
    <color name="white">#FFFFFF</color>
   <color name="black">#000000</color>
      <color name="red">#FF0000</color>
       <color name="gray">#cccccc</color>
</resources>



package com.example.cal;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_C;
Button btn_DEL;
Button btn_sin;
Button btn_genhao;
Button btn_jian;
Button btn_jia;
Button btn_chu;
Button btn_deng;
Button btn_dian;
Button btn_cheng;
EditText et_input;
boolean clear_flag;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn_0=(Button) this.findViewById(R.id.btn_0);
  btn_1=(Button) this.findViewById(R.id.btn_1);
  btn_2=(Button) this.findViewById(R.id.btn_2);
  btn_3=(Button) this.findViewById(R.id.btn_3);
  btn_4=(Button) this.findViewById(R.id.btn_4);
  btn_5=(Button) this.findViewById(R.id.btn_5);
  btn_6=(Button) this.findViewById(R.id.btn_6);
  btn_7=(Button) this.findViewById(R.id.btn_7);
  btn_8=(Button) this.findViewById(R.id.btn_8);
  btn_9=(Button) this.findViewById(R.id.btn_9);
  btn_C=(Button) this.findViewById(R.id.btn_C);
  btn_DEL=(Button) this.findViewById(R.id.btn_DEL);
  btn_sin=(Button) this.findViewById(R.id.btn_sin);
  btn_genhao=(Button) this.findViewById(R.id.btn_genhao);
  btn_jian=(Button) this.findViewById(R.id.btn_jian);
  btn_jia=(Button) this.findViewById(R.id.btn_jia);
  btn_chu=(Button) this.findViewById(R.id.btn_chu);
  btn_deng=(Button) this.findViewById(R.id.btn_deng);
  btn_dian=(Button) this.findViewById(R.id.btn_dian);
  btn_cheng=(Button) this.findViewById(R.id.btn_cheng);
  et_input=(EditText) this.findViewById(R.id.editText1);
 btn_0.setOnClickListener(this);
 btn_1.setOnClickListener(this);
 btn_2.setOnClickListener(this);
 btn_3.setOnClickListener(this);
 btn_4.setOnClickListener(this);
 btn_5.setOnClickListener(this);
 
 btn_6.setOnClickListener(this);
 btn_7.setOnClickListener(this);
 btn_8.setOnClickListener(this);
 btn_9.setOnClickListener(this);
 
 btn_C.setOnClickListener(this);
 btn_DEL.setOnClickListener(this);
 btn_sin.setOnClickListener(this);
 
 btn_jian.setOnClickListener(this);
 
 btn_jia.setOnClickListener(this);
 btn_chu.setOnClickListener(this);
 
 btn_deng.setOnClickListener(this);
 btn_dian.setOnClickListener(this);
 btn_cheng.setOnClickListener(this);
 btn_genhao.setOnClickListener(this);
 
 
 }
 @Override
 public void onClick(View v) {
 String str=et_input.getText().toString();
  switch(v.getId()){
  case R.id.btn_0:
  case R.id.btn_1:
  case R.id.btn_2:
  case R.id.btn_3:
  case R.id.btn_4:
  case R.id.btn_5:
  case R.id.btn_6:   
  case R.id.btn_7:
  case R.id.btn_8:   
  case R.id.btn_9:
  case R.id.btn_dian:
   if(clear_flag){clear_flag=false;
   str="";
   et_input.setText("");
   }
  et_input.setText(str+((Button)v).getText());
  break;
  
  case R.id.btn_jian:   
  case R.id.btn_jia:
  case R.id.btn_chu:
  case R.id.btn_cheng:
   if(clear_flag){clear_flag=false;
   str="";
   et_input.setText("");}
   et_input.setText(str+" "+((Button)v).getText()+" ");
   break;
   
  case R.id.btn_DEL:
   if(clear_flag){clear_flag=false;
   str="";
   et_input.setText("");}
   et_input.setText(str.substring(0, str.length()-1));
   break;
   
  case R.id.btn_C:
   clear_flag=false;
   str="";
   et_input.setText("");
   break;
   
  case R.id.btn_genhao:
   
   double n=Double.parseDouble(str);
   if(n<0)
   { 
    et_input.setText("");
   }else{
    et_input.setText(Math.sqrt(n)+"");
   }
   break;
   
  case R.id.btn_sin:
   double n1=Double.parseDouble(str);
  
    et_input.setText(Math.sin(n1)+"");
   
   break;
   
  case R.id.btn_deng:
   getResult();
   break;
  
  
   
  
   
 }
 }
 //运算结果
private void getResult(){
 String exp=et_input.getText().toString();
 if(exp==null||exp.equals("")){
  return;
 }
 if(!exp.contains(" ")){
  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 d1=Double.parseDouble(s1);
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
   result=d1+d2;
   
  }else if(op.equals("-")){
   result=d1-d2;
   
  }else if(op.equals("×")){
   result=d1*d2;
   
  }else if(op.equals("÷")){
   if(d2==0){
    result=0;
   }else{
    result=d1/d2;
   }  
  }
  
  if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
   int r=(int)result;
   et_input.setText(r+"");
  }else{
   et_input.setText(result+"");
  }
  
 }else if(!s1.equals("")&&s2.equals("")){
  et_input.setTag(exp);
 }else if(s1.equals("")&&!s2.equals("")){
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
   result=d2;
   
  }else if(op.equals("-")){
   result=-d2;
   
  }else if(op.equals("×")){
   result=0;
   
  }else if(op.equals("÷")){
  
    result=0;
   
  }
  if(!s2.equals(".")){
   int r=(int)result;
   et_input.setText(r+"");   
  }else{
 
   String reslut = null;
   et_input.setText(reslut+"");
  }
}else{
 et_input.setText("");
}
}
}
展示图不在了,尴尬……
附上名言

别人对你的态度,决定了你的命运。



G
M
T
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
Text-to-speech function is limited to 200 characters

猜你喜欢

转载自blog.csdn.net/weixin_38465623/article/details/78943420