<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/calculate" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"
android:hint="请用户输入"
></EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/X7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="@string/X7"/>
<Button
android:id="@+id/X8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X7"
android:layout_alignTop="@id/X7"
android:textSize="15pt"
android:text="@string/X8"/>
<Button
android:id="@+id/X9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X8"
android:layout_alignTop="@id/X8"
android:textSize="15pt"
android:text="@string/X9"/>
<Button
android:id="@+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X9"
android:layout_alignTop="@id/X9"
android:textSize="15pt"
android:text="@string/Add"/>
<Button
android:id="@+id/Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Add"
android:layout_alignTop="@id/Add"
android:textSize="15pt"
android:text="@string/Clear"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/X4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="@string/X4"/>
<Button
android:id="@+id/X5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X4"
android:layout_alignTop="@id/X4"
android:textSize="15pt"
android:text="@string/X5"/>
<Button
android:id="@+id/X6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X5"
android:layout_alignTop="@id/X5"
android:textSize="15pt"
android:text="@string/X6"/>
<Button
android:id="@+id/Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X6"
android:layout_alignTop="@id/X6"
android:textSize="15pt"
android:text="@string/Sub"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/X1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="@string/X1"/>
<Button
android:id="@+id/X2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X1"
android:layout_alignTop="@id/X1"
android:textSize="15pt"
android:text="@string/X2"/>
<Button
android:id="@+id/X3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X2"
android:layout_alignTop="@id/X2"
android:textSize="15pt"
android:text="@string/X3"/>
<Button
android:id="@+id/Mult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X3"
android:layout_alignTop="@id/X1"
android:textSize="15pt"
android:text="@string/Mult"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/Point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="@string/Point"/>
<Button
android:id="@+id/X0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Point"
android:layout_alignTop="@id/Point"
android:textSize="15pt"
android:text="@string/X0"/>
<Button
android:id="@+id/Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/X0"
android:layout_alignTop="@id/X0"
android:textSize="15pt"
android:text="@string/Calculate"/>
<Button
android:id="@+id/Divid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Calculate"
android:layout_alignTop="@id/Calculate"
android:textSize="15pt"
android:text="@string/Divid"/>
</RelativeLayout>
</LinearLayout>
package YY.android.calculate;
import android.app.Activity;
import android.os.Bundle;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CalculateActivity extends Activity {
private Button xAdd;
private Button xSub;
private Button xMult;
private Button xDivid;
private Button xCalculate;
private Button xClear;
private Button xPoint;
private EditText xedit;
private Button x1;
private Button x2;
private Button x3;
private Button x4;
private Button x5;
private Button x6;
private Button x7;
private Button x8;
private Button x9;
private Button x0;
private double result = 0;
private String flag = "Original";
private String Get;
private double one = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xAdd = (Button)findViewById(R.id.Add);
xSub = (Button)findViewById(R.id.Sub);
xMult = (Button)findViewById(R.id.Mult);
xDivid = (Button)findViewById(R.id.Divid);
xedit = (EditText)findViewById(R.id.edit);
xCalculate =(Button)findViewById(R.id.Calculate);
xClear = (Button)findViewById(R.id.Clear);
xPoint = (Button)findViewById(R.id.Point);
x1 = (Button)findViewById(R.id.X1);
x2 = (Button)findViewById(R.id.X2);
x3= (Button)findViewById(R.id.X3);
x4 = (Button)findViewById(R.id.X4);
x5 = (Button)findViewById(R.id.X5);
x6 = (Button)findViewById(R.id.X6);
x7 = (Button)findViewById(R.id.X7);
x8 = (Button)findViewById(R.id.X8);
x9= (Button)findViewById(R.id.X9);
x0 = (Button)findViewById(R.id.X0);
xAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get = xedit.getText().toString();
// one = Double.parseDouble(Get);
if(flag == "Original"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = one;
xedit.setText("");
}
else if(flag == "+"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result + one;
xedit.setText("");
}
else if(flag == "-"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result - one;
xedit.setText("");
}
else if(flag == "*"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result * one;
xedit.setText("");
}
else{
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result / one;
xedit.setText("");
}
flag = "+";
}
});
xSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Get = xedit.getText().toString();
//one = Double.parseDouble(Get);
if(flag == "Original"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = one;
xedit.setText("");
}
else if(flag == "+"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result + one;
xedit.setText("");
}
else if(flag == "-"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result - one;
xedit.setText("");
}
else if(flag == "*"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result * one;
xedit.setText("");
}
else{
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result / one;
xedit.setText("");
}
flag = "-";
}
});
xMult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get = xedit.getText().toString();
//one = Double.parseDouble(Get);
if(flag == "Original"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = one;
xedit.setText("");
}
else if(flag == "+"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result + one;
xedit.setText("");
}
else if(flag == "-"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result - one;
xedit.setText("");
}
else if(flag == "*"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result * one;
xedit.setText("");
}
else{
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result / one;
xedit.setText("");
}
flag = "*";
}
});
xDivid.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get = xedit.getText().toString();
// one = Double.parseDouble(Get);
if(flag == "Original"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = one;
xedit.setText("");
}
else if(flag == "+"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result + one;
xedit.setText("");
}
else if(flag == "-"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result - one;
xedit.setText("");
}
else if(flag == "*"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result * one;
xedit.setText("");
}
else{
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result / one;
xedit.setText("");
}
flag = "/";
}
});
xClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
xedit.setText(null);
flag = "Original";
}
});
xCalculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(flag == "+"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result + one;
xedit.setText(result+"");
result = 0;
flag = "Original";
}
else if(flag == "-"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result - one;
xedit.setText(result+"");
result = 0;
flag = "Original";
}
else if(flag == "*"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result * one;
xedit.setText(result+"");
result = 0;
flag = "Original";
}
else if(flag == "/"){
Get = xedit.getText().toString();
one = Double.parseDouble(Get);
result = result / one;
xedit.setText(result+"");
result = 0;
flag = "Original";
}
}
});
xPoint.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("第一个一定要是数字");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+".");
xedit.setSelection(Get.length()+1);
}
}
});
x1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("1");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"1");
xedit.setSelection(Get.length()+1);
}
}
});
x2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("2");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"2");
xedit.setSelection(Get.length()+1);
}
}
});
x3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("3");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"3");
xedit.setSelection(Get.length()+1);
}
}
});
x4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("4");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"4");
xedit.setSelection(Get.length()+1);
}
}
});
x5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("5");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"5");
xedit.setSelection(Get.length()+1);
}
}
});
x6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("6");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"6");
xedit.setSelection(Get.length()+1);
}
}
});
x7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("7");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"7");
xedit.setSelection(Get.length()+1);
}
}
});
x8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("8");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"8");
xedit.setSelection(Get.length()+1);
}
}
});
x9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("9");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"9");
xedit.setSelection(Get.length()+1);
}
}
});
x0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if(null == xedit.getText().toString())
if(null == xedit.getText()){
xedit.setText("0");
}
else{
Get = xedit.getText().toString();
xedit.setText(Get+"0");
xedit.setSelection(Get.length()+1);
}
}
});
}
}