简单计算机.java代码
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
String str, num1, num2;
private double result;
private boolean addNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.textView);
addNum=true;
}
@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);
}
public void onClick(View v){
str = (String) textView.getText();
switch (v.getId()) {
case R.id.btn_cle:
textView.setText("");
break;
case R.id.btn_del:
if(!str.equals("") && str != null){
textView.setText(str.substring(0, str.length()-1));
}
break;
case R.id.btn_equ:
if(str.contains("+")){
getResult(num1, num2, "+");
}
else if(str.contains("-")){
getResult(num1, num2, "-");
}
else if(str.contains("×")){
getResult(num1, num2, "×");
}
else if(str.contains("÷")){
getResult(num1, num2, "÷");
}
else {
return;
}
break;
case R.id.btn_add:
case R.id.btn_sub:
case R.id.btn_mul:
case R.id.btn_div:
if (str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷"))
return;
else
textView.setText(str+((Button)v).getText());
if(!addNum)
addNum = true;
break;
default:
if (addNum) {
textView.setText(str+((Button)v).getText());
}else{
textView.setText(str+((Button)v).getText());
addNum = true;
}
break;
}
}
private void getResult(String num1,String num2,String op) {
num1 = str.substring(0,str.indexOf(op));
num2 = str.substring(str.indexOf(op)+1);
try {
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
if (op.equals("+")) {
result = n1+n2;
}else if(op.equals("-")){
result = n1-n2;
}else if(op.equals("×")){
result = n1*n2;
}else if(op.equals("÷")){
result = n1/n2;
}else {
return;
}
String r = result+"";
if(r.contains(".")&&r.substring(r.length()-1).equals("0")){
r = r.substring(0,r.indexOf("."));
}
textView.setText(r);
addNum = false;
} catch (Exception e) {
return;
}
}
}
xml 文件
android:id="@+id/LinearLayout1"
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" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:height="40dp"
android:textSize="30sp" />
android:layout_height="wrap_content"
android:stretchColumns="*" >
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="7" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="8" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="9" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="÷" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="4" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="5" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="6" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="×" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="2" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="3" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="-" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="." />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="0" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="=" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="+" />