由于是初学者,我只是做了对其做了简单的处理,还有一些BUG没能处理好,当然,不影响简单运算的使用。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:rowCount="7"
android:columnCount="4" >
<TextView
android:id="@+id/outcome1"
android:layout_columnSpan="4"
android:layout_gravity="fill"
/>
<TextView
android:id="@+id/outcome"
android:layout_columnSpan="4"
android:layout_gravity="fill"
/>
<Button
android:id="@+id/one"
android:text="1"
/>
<Button
android:id="@+id/two"
android:text="2"
/>
<Button
android:id="@+id/three"
android:text="3"
/>
<Button
android:id="@+id/devide"
android:text="/"
/>
<Button
android:id="@+id/four"
android:text="4"
/>
<Button
android:id="@+id/five"
android:text="5"
/>
<Button
android:id="@+id/six"
android:text="6"
/>
<Button
android:id="@+id/multiply"
android:text="*"
/>
<Button
android:id="@+id/seven"
android:text="7"
/>
<Button
android:id="@+id/eight"
android:text="8"
/>
<Button
android:id="@+id/nine"
android:text="9"
/>
<Button
android:id="@+id/minus"
android:text="-"
/>
<Button
android:id="@+id/zero"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0"
/>
<Button
android:id="@+id/point"
android:text="C"
/>
<Button
android:id="@+id/plus"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:text="+"
/>
<Button
android:id="@+id/equal"
android:text="="
android:layout_columnSpan="3"
android:layout_gravity="fill"
/>
</GridLayout>
java代码:
package com.example.calculator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button one,two,three,four,five,six,seven,eight,nine,zero,multiply,minus,devide,plus,point,equal;
TextView outcome,outcome1;
String showString="",option="";
String exception="";
double showfirst=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
zero = (Button) findViewById(R.id.zero);
devide = (Button) findViewById(R.id.devide);
multiply = (Button) findViewById(R.id.multiply);
minus = (Button) findViewById(R.id.minus);
plus = (Button) findViewById(R.id.plus);
point = (Button) findViewById(R.id.point);
equal = (Button) findViewById(R.id.equal);
outcome = (TextView) findViewById(R.id.outcome);
outcome1=(TextView) findViewById(R.id.outcome1);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
zero.setOnClickListener(this);
devide.setOnClickListener(this);
multiply.setOnClickListener(this);
minus.setOnClickListener(this);
plus.setOnClickListener(this);
point.setOnClickListener(this);
equal.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_item:
Intent intent=new Intent(MainActivity.this,second.class);
startActivity(intent);
break;
case R.id.add_remove:
Toast.makeText(this,"已经是最新版本",Toast.LENGTH_SHORT).show();
default:
}
return true;
}
@SuppressLint("ResourceType")
public void onClick(View v){
switch (v.getId()){
case R.id.zero:
showString+="0";
outcome1.setText(showString);
break;
case R.id.one:
showString+="1";
break;
case R.id.two:
showString+="2";
break;
case R.id.three:
showString+="3";
break;
case R.id.four:
showString+="4";
break;
case R.id.five:
showString+="5";
break;
case R.id.six:
showString+="6";
break;
case R.id.seven:
showString+="7";
break;
case R.id.eight:
showString+="8";
break;
case R.id.nine:
showString+="9";
break;
case R.id.plus:
if(showString.equals(""))
exception="先输入数据哦";
else{
option="+";
showfirst=Double.parseDouble(showString);
showString="";
}
break;
case R.id.minus:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="-";
}
break;
case R.id.multiply:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="*";
}
break;
case R.id.devide:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="/";
}
break;
case R.id.equal:
if(option.equals("+")) {
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString = showfirst +Double.parseDouble(showString)+"";
option="";
showfirst=0;
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("-")){
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst-Double.parseDouble(showString)+"";
option="";
showfirst=0;
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("*")){
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst*Double.parseDouble(showString)+"";
option="";
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("/")){
if(showString.equals("0")){
exception="除数不能为0!";
Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show();
}else if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst/Double.parseDouble(showString)+"";
option="";
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.point:
showString="";
outcome.setText("");
outcome1.setText("");
break;
default:
break;
}
if(exception.equals("")) {
outcome.setText(showString);
}
else{
outcome.setText(showString);
exception="";
}
}
}
效果图