第一个安卓程序设计------简易计算器
MainActivity
package com.example.a18229.calculator;
import android.view.View.OnClickListener;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import java.lang.Math;
import java.lang.String;
public class MainActivityextends AppCompatActivityimplements OnClickListener {
Buttonone, two, three, four, five, six, seven, eight, nine, zero, div, add, sub, mul, point, zf, equal,clear, clearall,sqrt;
TextViewshow;
Stringa="",c="";
boolean isFirst=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
one.setOnClickListener(this);
two = (Button) findViewById(R.id.two);
two.setOnClickListener(this);
three = (Button) findViewById(R.id.three);
three.setOnClickListener(this);
four = (Button) findViewById(R.id.four);
four.setOnClickListener(this);
five = (Button) findViewById(R.id.five);
five.setOnClickListener(this);
six = (Button) findViewById(R.id.six);
six.setOnClickListener(this);
seven = (Button) findViewById(R.id.seven);
seven.setOnClickListener(this);
eight = (Button) findViewById(R.id.eight);
eight.setOnClickListener(this);
nine = (Button) findViewById(R.id.nine);
nine.setOnClickListener(this);
zero = (Button) findViewById(R.id.zero);
zero.setOnClickListener(this);
zf = (Button) findViewById(R.id.zf);
zf.setOnClickListener(this);
div = (Button) findViewById(R.id.div);
div.setOnClickListener(this);
add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
sub = (Button) findViewById(R.id.sub);
sub.setOnClickListener(this);
mul = (Button) findViewById(R.id.mul);
mul.setOnClickListener(this);
point = (Button) findViewById(R.id.point);
point.setOnClickListener(this);
equal = (Button) findViewById(R.id.equal);
equal.setOnClickListener(this);
show = (TextView) findViewById(R.id.show);
show.setOnClickListener(this);
clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(this);
clearall = (Button) findViewById(R.id.clearall);
clearall.setOnClickListener(this);
sqrt = (Button) findViewById(R.id.sqrt);
sqrt.setOnClickListener(this);
zf = (Button)findViewById(R.id.zf);
zf.setOnClickListener(this);
}
public void Reset()
{
a="";
c="";
}
public void ClearScreen()
{
show.setText("0");
isFirst =true;
}
public void ClearAll()
{
ClearScreen();
Reset();
}
public void Setshow(String str)
{
if(show.getText().toString().indexOf(".")>0 && str.equals(".")) {return;}
if(isFirst){
if(str.equals(".")){
show.setText(show.getText()+str);
isFirst =false;
return;
}
else{
show.setText(str);
isFirst =false;
return;
}
}
show.setText(show.getText()+str);
}
public void GetParam(String str)
{
if(c.equals("")){
c = str;
a =show.getText().toString();
ClearScreen();
}
else{
Calculate();
}
}
public void Calculate()
{
if(a.equals(""))return;
boolean isDouble=false;
if(a.indexOf(".")>0 ||show.getText().toString().indexOf(".")>0){
isDouble =true;
}
switch (c.toCharArray()[0]){
case '+':
if(isDouble){
show.setText(String.valueOf(Double.parseDouble(a)+Double.parseDouble(show.getText().toString())));
Reset();
break;
}
show.setText(String.valueOf(Integer.parseInt(a)+Integer.parseInt(show.getText().toString())));
Reset();
break;
case '-':
if(isDouble){
show.setText(String.valueOf(Double.parseDouble(a)-Double.parseDouble(show.getText().toString())));
Reset();
break;
}
show.setText(String.valueOf(Integer.parseInt(a)-Integer.parseInt(show.getText().toString())));
Reset();
break;
case '*':
if(isDouble){
show.setText(String.valueOf(Double.parseDouble(a)*Double.parseDouble(show.getText().toString())));
Reset();
break;
}
show.setText(String.valueOf(Integer.parseInt(a)*Integer.parseInt(show.getText().toString())));
Reset();
break;
case '/':
if(isDouble){
show.setText(String.valueOf(Double.parseDouble(a)/Double.parseDouble(show.getText().toString())));
Reset();
break;
}
show.setText(String.valueOf((double)(Integer.parseInt(a)/Integer.parseInt(show.getText().toString()))));
Reset();
break;
}
}
public void clSqrt()
{
show.setText(String.valueOf(Math.sqrt(Double.parseDouble(show.getText().toString()))));
}
public void changec()
{
if(show.getText().toString().indexOf(".")>0){
show.setText(String.valueOf(-Double.parseDouble(show.getText().toString())));
}
else{
show.setText(String.valueOf(-Integer.parseInt(show.getText().toString())));
}
}
public void onClick(View v) {
switch (v.getId()){
case R.id.one:
Setshow("1");
break;
case R.id.two:
Setshow("2");
break;
case R.id.three:
Setshow("3");
break;
case R.id.four:
Setshow("4");
break;
case R.id.five:
Setshow("5");
break;
case R.id.six:
Setshow("6");
break;
case R.id.seven:
Setshow("7");
break;
case R.id.eight:
Setshow("8");
break;
case R.id.nine:
Setshow("9");
break;
case R.id.zero:
Setshow("0");
break;
case R.id.point:
Setshow(".");
break;
case R.id.clear:
ClearScreen();
break;
case R.id.add:
GetParam("+");
break;
case R.id.sub:
GetParam("-");
break;
case R.id.mul:
GetParam("*");
break;
case R.id.div:
GetParam("/");
break;
case R.id.equal:
Calculate();
break;
case R.id.clearall:
ClearAll();
break;
case R.id.sqrt:
clSqrt();
break;
case R.id.zf:
changec();
break;
}
}
}
xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:rowCount="7"
android:columnCount="4">
android:layout_columnSpan="4"
android:layout_row="0"
android:layout_gravity="center_horizontal"
android:text="计算器" />
android:id="@+id/show"
android:layout_width="130dip"
android:layout_columnSpan="4"
android:layout_row="0"
android:text="0"
android:layout_gravity="fill_horizontal"
android:gravity="right"/>
android:layout_width="72dip"
android:id="@+id/clear"
android:text="C"/>
android:layout_width="72dip"
android:id="@+id/clearall"
android:text="CE"/>
android:layout_width="72dip"
android:id="@+id/sqrt"
android:text="√"/>
android:layout_width="72dip"
android:id="@+id/add"
android:text="+"/>
android:layout_width="72dip"
android:id="@+id/one"
android:text="1"/>
android:layout_width="72dip"
android:id="@+id/two"
android:text="2"/>
android:layout_width="72dip"
android:id="@+id/three"
android:text="3"/>
android:layout_width="72dip"
android:id="@+id/sub"
android:text="-"/>
android:layout_width="72dip"
android:id="@+id/four"
android:text="4"/>
android:layout_width="72dip"
android:id="@+id/five"
android:text="5"/>
android:layout_width="72dip"
android:id="@+id/six"
android:text="6"/>
android:layout_width="72dip"
android:id="@+id/mul"
android:text="*"/>
android:layout_width="72dip"
android:id="@+id/seven"
android:text="7"/>
android:layout_width="72dip"
android:id="@+id/eight"
android:text="8"/>
android:layout_width="72dip"
android:id="@+id/nine"
android:text="9"/>
android:layout_width="72dip"
android:id="@+id/div"
android:text="/"/>
android:layout_width="72dip"
android:id="@+id/point"
android:text="."/>
android:layout_width="72dip"
android:id="@+id/zero"
android:text="0"/>
android:layout_width="72dip"
android:id="@+id/zf"
android:text="±"/>
android:layout_width="72dip"
android:id="@+id/equal"
android:text="="/>