安卓简易计算器

这是一个可以实现简单的加减乘除的计算器,代码比较简单微笑


这是布局

安卓简易计算器_第1张图片


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical"
    android:background="@color/colorGray"
    tools:context="custom.community.com.con_deom.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"

    >
  <EditText
      android:id="@+id/edt1"
      android:layout_width="100dp"
    android:layout_height="40dp"
    android:background="@color/colorWhite"
      android:layout_marginRight="10dp"
      android:singleLine="true"
     android:inputType="numberDecimal"
      android:padding="2dp"

      android:maxLength="8"

    />
    <TextView
        android:id="@+id/txt_num"
        android:layout_width="40dp"
        android:layout_height="34dp"
        android:gravity="center"
        android:text=""

        android:background="@color/colorAccent"
        android:textSize="18dp"
        />
    <EditText
        android:id="@+id/edt2"
        android:layout_marginLeft="10dp"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:singleLine="true"
        android:inputType="numberDecimal"
        android:maxLength="8"
        android:padding="2dp"
        android:background="@color/colorWhite"

        />
    <Button
        android:id="@+id/btn_clean"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:background="@color/colorAccent"

        android:text="清零"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        >

        <TextView
            android:id="@+id/txt_result"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:text="="
            android:gravity="center"
            android:background="@color/colorAccentr"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textSize="18dp"
            android:layout_weight="1"
            />
        <TextView
            android:id="@+id/txt_resultnum"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="18dp"
            android:textColor="#000"
            android:layout_gravity="center"
            android:background="@color/colorWhite"
            />
    </LinearLayout>
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"

       android:layout_marginTop="20dp"
       >
      <Button
          android:id="@+id/btn_jia"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:background="@color/colorAccent"
          android:text="+"
          android:layout_weight="1"

          />
       <Button
           android:id="@+id/btn_jian"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@color/colorAccent"
           android:text="-"
           android:layout_weight="1"
           android:layout_marginLeft="5dp"
           />
       <Button
           android:id="@+id/btn_che"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@color/colorAccent"
           android:text="*"
           android:layout_weight="1"
           android:layout_marginLeft="5dp"
           />
       <Button
           android:id="@+id/btn_chu"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@color/colorAccent"
           android:text="/"
           android:layout_weight="1"
           android:layout_marginLeft="5dp"
           />
   </LinearLayout>
</LinearLayout>



下面的是源码,但思路比较简单


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText edt1,edt2;
    private TextView txt_num,txt_result,txt_resultnum;
    private Button btn_jia,btn_jian,btn_che,btn_chu,btn_clean;
    private  int isOnclick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        isOnclick=0;
        edt1=(EditText)findViewById(R.id.edt1);
        edt2=(EditText)findViewById(R.id.edt2);
        txt_num=(TextView)findViewById(R.id.txt_num);
        txt_result=(TextView)findViewById(R.id.txt_result);
        txt_resultnum=(TextView)findViewById(R.id.txt_resultnum);
        btn_jia=(Button)findViewById(R.id.btn_jia);
        btn_jian=(Button)findViewById(R.id.btn_jian);
        btn_che=(Button)findViewById(R.id.btn_che);
        btn_chu=(Button)findViewById(R.id.btn_chu);
        btn_clean=(Button)findViewById(R.id.btn_clean);
        btn_jia.setOnClickListener(MainActivity.this);
        btn_jian.setOnClickListener(MainActivity.this);
        btn_che.setOnClickListener(MainActivity.this);
        btn_chu.setOnClickListener(MainActivity.this);
        btn_clean.setOnClickListener(MainActivity.this);

        //点等于号算出结果
        txt_result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edt1.getText().toString().equals("") || edt2.getText().toString().equals("")) {
                    isEmpty_num();
                } else {

                    String numCount;
                    Float   t1= Float.parseFloat(edt1.getText().toString());
                    Float  t2= Float.parseFloat(edt2.getText().toString());
                  switch (isOnclick){
                      case 1:
                          numCount=Float.toString(t1+t2);
                          txt_resultnum.setText(numCount);
                          break;
                      case 2:
                          numCount=Float.toString(t1-t2);
                          txt_resultnum.setText(numCount);
                          break;
                      case 3:
                          numCount=Float.toString(t1*t2);
                          txt_resultnum.setText(numCount);
                          break;
                      case 4:
                          numCount=Float.toString(t1/t2);
                          txt_resultnum.setText(numCount);
                          break;
                      default:
                          Toast.makeText(MainActivity.this,"请输入运算符号",Toast.LENGTH_LONG).show();
                          break;

                  }

                }
            }
        });


    }
    @Override
    public void onClick(View v) {
        String edt1_num=edt1.getText().toString();
        String edt2_num=edt2.getText().toString();
        switch (v.getId()) {
            //加
            case R.id.btn_jia:
                if (edt1_num.equals("") && edt2_num.equals("")) {
                    isEmpty_num();
                } else {
                    txt_num.setText("+");
                    edt1.clearFocus();
                    edt2.requestFocus();
                    isOnclick=1;
                }
                break;
            //减
            case R.id.btn_jian:
                if (edt1_num.equals("") && edt2_num.equals("")) {
                    isEmpty_num();
                } else {
                    txt_num.setText("-");

                    edt1.clearFocus();
                    edt2.requestFocus();
                    isOnclick=2;
                }
                break;
            //乘
            case R.id.btn_che:
                if (edt1_num.equals("") && edt2_num.equals("")) {
                    isEmpty_num();
                } else {
                    txt_num.setText("*");

                    edt1.clearFocus();
                    edt2.requestFocus();
                    isOnclick=3;
                }
                break;
            //除
            case R.id.btn_chu:
                if (edt1_num.equals("") && edt2_num.equals("")) {
                    isEmpty_num();
                } else {
                    txt_num.setText("/");
                    edt1.clearFocus();
                    edt2.requestFocus();
                    isOnclick=4;
                }
                break;
            //清零
            case R.id.btn_clean:
                edt1.setText("");
                edt2.setText("");
                txt_resultnum.setText("");
                edt1.requestFocus();
                edt2.clearFocus();
                break;
            default:
                break;
        }
    }

   //弹框
    public void isEmpty_num(){
        Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_LONG).show();

    }
}





你可能感兴趣的:(计算器源码,安卓计算器,安卓简单计算器)