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
layout_port文件夹中 testland.xml
drawable 文件夹中border.xml
border1.xml
border2.xml