- 完成一个计算器的设计,可以以手机自带的计算器为参考。设计过程中,注意考虑界面的美观性,不同机型的适应性,以及功能的完备性。
- 注意结合Activity的生命周期,考虑不同情况下计算器的界面状态。
- 如有余力,可以考虑实现一个高精度科学计算型的计算器。
MainActivity.java
package com.example.myapplication;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.util.Log;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final static String Tag = "LifeCycle";
EditText result;
Button button_clean, button_divide, button_mutiply, button_cleanError,
button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9,
button_subtract, button_plus, button_equal, button_percent, button_point;
boolean clear_flag;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.testland);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testland);
Log.i(Tag, "onCreate()");
result = (EditText) findViewById(R.id.result);
button_clean = (Button) findViewById(R.id.button_clean);
button_divide = (Button) findViewById(R.id.button_divide);
button_mutiply = (Button) findViewById(R.id.button_mutiply);
button_cleanError = (Button) findViewById(R.id.button_cleanError);
button_0 = (Button) findViewById(R.id.button_0);
button_1 = (Button) findViewById(R.id.button_1);
button_2 = (Button) findViewById(R.id.button_2);
button_3 = (Button) findViewById(R.id.button_3);
button_4 = (Button) findViewById(R.id.button_4);
button_5 = (Button) findViewById(R.id.button_5);
button_6 = (Button) findViewById(R.id.button_6);
button_7 = (Button) findViewById(R.id.button_7);
button_8 = (Button) findViewById(R.id.button_8);
button_9 = (Button) findViewById(R.id.button_9);
button_subtract = (Button) findViewById(R.id.button_subtract);
button_plus = (Button) findViewById(R.id.button_plus);
button_equal = (Button) findViewById(R.id.button_equal);
button_percent = (Button) findViewById(R.id.button_percent);
button_point = (Button) findViewById(R.id.button_point);
result.setOnClickListener(this);
button_clean.setOnClickListener(this);
button_divide.setOnClickListener(this);
button_mutiply.setOnClickListener(this);
button_cleanError.setOnClickListener(this);
button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
button_subtract.setOnClickListener(this);
button_plus.setOnClickListener(this);
button_equal.setOnClickListener(this);
button_percent.setOnClickListener(this);
button_point.setOnClickListener(this);
}
@Override
public void onStart(){
super.onStart();
Log.i(Tag,"onStart()");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
Log.i(Tag,"onRestoreInstanceState()");
}
@Override
public void onResume() {
super.onResume();
Log.i(Tag,"onResume()");
}
@Override
public void onRestart(){
super.onRestart();
Log.i(Tag,"onRestart()");
}
@Override
public void onPause(){
super.onPause();
Log.i(Tag,"onPause()");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(Tag,"onSaveInstanceState()");
}
@Override
public void onStop(){
super.onStop();
Log.i(Tag,"onStop()");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.i(Tag,"onDestroy()");
}
@Override
public void onClick(View view) {
String str_result = result.getText().toString();
switch (view.getId()) {
case R.id.button_0:
case R.id.button_1:
case R.id.button_2:
case R.id.button_3:
case R.id.button_4:
case R.id.button_5:
case R.id.button_6:
case R.id.button_7:
case R.id.button_8:
case R.id.button_9:
case R.id.button_point:
if (clear_flag) {
clear_flag = false;
str_result = "";
result.setText("");
}
result.setText(str_result + ((Button) view).getText());
break;
case R.id.button_divide:
case R.id.button_mutiply:
case R.id.button_subtract:
case R.id.button_percent:
case R.id.button_plus:
if (clear_flag) {
clear_flag = false;
str_result = "";
result.setText("");
}
result.setText(str_result + " " + ((Button) view).getText() + " ");
break;
case R.id.button_clean:
clear_flag = false;
result.setText("");
break;
case R.id.button_cleanError:
if (clear_flag) {
clear_flag = false;
str_result = "";
result.setText("");
} else if (str_result != null && !str_result.equals("") && !str_result.endsWith(" "))//删除运算数
result.setText(str_result.substring(0, str_result.length() - 1));
else if (str_result.endsWith(" "))//删除运算符
result.setText(str_result.substring(0, str_result.length() - 3));
break;
case R.id.button_equal:
getResult();
break;
default:
break;
}
}
private void getResult() {
String exp = result.getText().toString();
if (exp == null || exp.equals("") || !exp.contains(" ")) {
return;
}
if (clear_flag) {
clear_flag = false;
result.setText("");
}
clear_flag = true;
double calculation = 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("+")) {
calculation = d1 + d2;
} else if (op.equals("—")) {
calculation = d1 - d2;
} else if (op.equals("×")) {
calculation = d1 * d2;
} else if (op.equals("÷")) {
if (d2 == 0) {
calculation = 0;
} else {
calculation = d1 / d2;
}
}
if (!s1.contains(".") && !s2.contains(".") && !op.equals("÷")) {
int c = (int) calculation;
result.setText(c + "");
} else {
result.setText(calculation + "");
}
} else if (!s1.equals("") && s2.equals("")) {
result.setText(exp);
} else if (s1.equals("") && !s2.equals("")) {
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
calculation = 0 + d2;
} else if (op.equals("—")) {
calculation = 0 - d2;
} else if (op.equals("×") || op.equals("÷")) {
calculation = 0;
} else if (op.equals("%")) {
calculation = d2 / 100;
}
if (!s2.contains(".") && !op.equals("%")) {
int c = (int) calculation;
result.setText(c + "");
} else {
result.setText(calculation + "");
}
} else {
result.setText("");
}
}
}
layout_land文件夹中 testland.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<EditText
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8"
android:background="@null"
android:gravity="right|bottom"
android:cursorVisible="false"
android:paddingBottom="35dip"
android:textColor="#5c5c5c"
android:textSize="35sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:id="@+id/button_mc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="mc"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_Mplus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="m+"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_Msubtract"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="m-"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_mr"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="mr"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:id="@+id/button_clean"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_divide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
<Button
android:id="@+id/button_mutiply"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="×"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
<Button
android:id="@+id/button_cleanError"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CE"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="7"
android:textSize="25sp" />
<Button
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="8"
android:textSize="25sp" />
<Button
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="9"
android:textSize="25sp" />
<Button
android:id="@+id/button_subtract"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="—"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="4"
android:textSize="25sp" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="5"
android:textSize="25sp" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="6"
android:textSize="25sp" />
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="1"
android:textSize="25sp" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="2"
android:textSize="25sp" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="3"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_percent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="%"
android:textColor="#3A5FCD"
android:textSize="30sp" />
<Button
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="0"
android:textSize="25sp" />
<Button
android:id="@+id/button_point"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="."
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_equal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3A5FCD"
android:text="="
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
layout_port文件夹中 testland.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<EditText
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8"
android:background="@null"
android:gravity="right|bottom"
android:cursorVisible="false"
android:paddingBottom="35dip"
android:textColor="#5c5c5c"
android:textSize="35sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:id="@+id/button_mc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="mc"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_Mplus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="m+"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_Msubtract"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="m-"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_mr"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="mr"
android:textColor="#757677"
android:background="@drawable/border1"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:id="@+id/button_clean"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="25sp" />
<Button
android:id="@+id/button_divide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
<Button
android:id="@+id/button_mutiply"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="×"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
<Button
android:id="@+id/button_cleanError"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="CE"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="7"
android:textSize="25sp" />
<Button
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="8"
android:textSize="25sp" />
<Button
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="9"
android:textSize="25sp" />
<Button
android:id="@+id/button_subtract"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="—"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="4"
android:textSize="25sp" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="5"
android:textSize="25sp" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="6"
android:textSize="25sp" />
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textColor="#3A5FCD"
android:background="@drawable/border1"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="1"
android:textSize="25sp" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="2"
android:textSize="25sp" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="3"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_percent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="%"
android:textColor="#3A5FCD"
android:textSize="30sp" />
<Button
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:text="0"
android:textSize="25sp" />
<Button
android:id="@+id/button_point"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_weight="1"
android:text="."
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_equal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3A5FCD"
android:text="="
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
drawable 文件夹中border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="0.01dp"
android:color="#e3e1e1" />
<margin
android:bottom="-7dp"
android:left="-7dp"
android:right="-7dp"
android:top="-7dp" />
</shape>
border1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f0eeee"/>
<stroke
android:width="0.01dp"
android:color="#e3e1e1" />
<margin
android:bottom="-7dp"
android:left="-7dp"
android:right="-7dp"
android:top="-7dp" />
</shape>
border2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#d9d8d8"/>
<stroke
android:width="0.01dp"
android:color="#e3e1e1" />
<margin
android:bottom="-7dp"
android:left="-7dp"
android:right="-7dp"
android:top="-7dp" />
</shape>