• android简易计算器


    activity_main.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="match_parent"
    tools:context="com.example.demo07.MainActivity"
    android:background="#000000"
    android:orientation="vertical"
    >

    <EditText
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:id="@+id/et_input"
    android:background="@drawable/white_bg"
    android:editable="false"
    android:gravity="right|bottom"
    />
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="C"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_clear"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="DEL"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_del"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="/"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_divide"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="*"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_multiply"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    </LinearLayout>

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="7"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_7"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="8"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_8"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="9"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_9"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="-"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_minus"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    </LinearLayout>

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="4"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_4"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="5"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_5"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="6"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_6"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="+"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_plus"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    </LinearLayout>

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    >
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="1"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_1 "
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="2"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_2"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="3"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_3"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />


    </LinearLayout>
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    >
    <Button
    android:layout_width="130dp"
    android:layout_height="55dp"
    android:text="0"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_0 "
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    <Button
    android:layout_width="60dp"
    android:layout_height="55dp"
    android:text="."
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_point"
    android:background="@drawable/white_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    </LinearLayout>
    </LinearLayout>
    <Button
    android:layout_width="60dp"
    android:layout_height="120dp"
    android:text="="
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:gravity="right|bottom"
    android:id="@+id/btn_equal"
    android:background="@drawable/orange_selector"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />
    </LinearLayout>

    </LinearLayout>

    MainActivity.java:

    package com.example.demo07;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;


    public class MainActivity extends ActionBarActivity 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_point;
    Button btn_clear;
    Button btn_del;
    Button btn_plus;
    Button btn_minus;
    Button btn_multiply;
    Button btn_divide;
    Button btn_equal;

    EditText et_input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_0=(Button) findViewById(R.id.btn_0);
    btn_1=(Button) findViewById(R.id.btn_1);
    btn_2=(Button) findViewById(R.id.btn_2);
    btn_3=(Button) findViewById(R.id.btn_3);
    btn_4=(Button) findViewById(R.id.btn_4);
    btn_5=(Button) findViewById(R.id.btn_5);
    btn_6=(Button) findViewById(R.id.btn_6);
    btn_7=(Button) findViewById(R.id.btn_7);
    btn_8=(Button) findViewById(R.id.btn_8);
    btn_9=(Button) findViewById(R.id.btn_9);
    btn_point=(Button) findViewById(R.id.btn_point);
    btn_clear=(Button) findViewById(R.id.btn_clear);
    btn_del=(Button) findViewById(R.id.btn_del);
    btn_plus=(Button) findViewById(R.id.btn_plus);
    btn_minus=(Button) findViewById(R.id.btn_minus);
    btn_multiply=(Button) findViewById(R.id.btn_multiply);
    btn_divide=(Button) findViewById(R.id.btn_divide);
    btn_equal=(Button) findViewById(R.id.btn_equal);
    //以上是实例化按钮
    et_input=(EditText) findViewById(R.id.et_input);

    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_point.setOnClickListener(this);
    btn_clear.setOnClickListener(this);
    btn_del.setOnClickListener(this);
    btn_plus.setOnClickListener(this);
    btn_minus.setOnClickListener(this);
    btn_multiply.setOnClickListener(this);
    btn_divide.setOnClickListener(this);
    btn_equal.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }


    @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_point:
    et_input.setText(str+((Button)v).getText());
    break;
    case R.id.btn_clear:
    et_input.setText("");
    break;
    case R.id.btn_del:
    if(str!=null&&!str.equals("")){
    et_input.setText(str.substring(0,str.length()-1));
    }
    break;
    case R.id.btn_plus:
    case R.id.btn_minus:
    case R.id.btn_multiply:
    case R.id.btn_divide:
    et_input.setText(str+" "+((Button)v).getText()+" ");
    break;
    case R.id.btn_equal:
    getResult();
    break;
    }
    }
    private void getResult(){
    String exp=et_input.getText().toString();
    if(exp==null||exp.equals("")){
    return;
    }
    if(!exp.contains(" ")){
    return;
    }
    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(".")){
    int r=(int)result;
    et_input.setText(r+"");
    }else{
    et_input.setText(result+"");
    }
    }else if(!s1.equals("")&&s2.equals("")){
    et_input.setText(exp);
    }else if(s1.equals("")&&!s2.equals("")){
    double d2=Double.parseDouble(s2);
    if(op.equals("+")){
    result=0+d2;
    }else if(op.equals("-")){
    result=0-d2;
    }else if(op.equals("*")){
    result=0;
    }else if(op.equals("/")){
    result=0;
    }
    if(!s2.contains(".")){
    int r=(int)result;
    et_input.setText(r+"");
    }else{
    et_input.setText(result+"");
    }
    }else{
    et_input.setText("");
    }
    }
    }

  • 相关阅读:
    VC编译器
    位域
    位域2
    函数调用时刻的堆栈情况
    字节对齐2
    Python学习笔记(6)while循环、while嵌套
    Python学习笔记(7)字符串Str
    Python学习笔记(3)输出、输入、输出输入综合小练习
    Python学习笔记(1)Python介绍、解释器、第一个python程序、注释
    Python学习笔记(4)运算符、运算符小练习
  • 原文地址:https://www.cnblogs.com/daochong/p/4907643.html
Copyright © 2020-2023  润新知