Android实现简单的计算器功能
前言:通过Android实现简单的计算器功能,实现简单的加、减、乘、除操作。
效果图如下:
第一步
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="10dp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="10dp"tools:context="com.newdegree.calculator.MainActivity"
><EditTextandroid:id="@+id/et_input"android:layout_width="fill_parent"android:layout_height="60dp"android:inputType="none"android:gravity="center|right"android:background="#f0f0f0"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_clear"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_del"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="DEL"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_divide"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="/"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_multply"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="*"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="7"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="8"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="9"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_minus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="-"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="4"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="5"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="6"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_plus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="+"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="2"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="3"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="10dp"android:weightSum="1"><Buttonandroid:id="@+id/btn_0"android:layout_width="180dp"android:layout_height="wrap_content"android:text="0"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_point"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_weight="18.10"android:text="."android:textSize="20sp"/></LinearLayout></LinearLayout><Buttonandroid:id="@+id/btn_equal"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="5dp"android:background="@android:color/holo_orange_light"android:text="="android:textSize="20sp"/></LinearLayout></LinearLayout>
布局文件中定义了计算器界面的一些按钮和显示数字的组件。
- 第二步
MainActivity.java
publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener {private Button btn_0;//0数字按钮private Button btn_1;//1数字按钮private Button btn_2;//2数字按钮private Button btn_3;//3数字按钮private Button btn_4;//4数字按钮private Button btn_5;//5数字按钮private Button btn_6;//6数字按钮private Button btn_7;//7数字按钮private Button btn_8;//8数字按钮private Button btn_9;//9数字按钮private Button btn_point;//小数点按钮private Button btn_clear;//clear按钮private Button btn_del;//del按钮private Button btn_plus;//+按钮private Button btn_minus;//-按钮private Button btn_multply;//*按钮private Button btn_divide;//除号按钮private Button btn_equal;//=按钮private EditText editText;boolean clear_flag;//清空标识@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_plus = (Button) findViewById(R.id.btn_plus);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_multply = (Button) findViewById(R.id.btn_multply);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_equal = (Button) findViewById(R.id.btn_equal);
editText = (EditText) findViewById(R.id.et_input);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
}@OverridepublicvoidonClick(View view) {
String input = editText.getText().toString();switch (view.getId()){case R.id.btn_0:case R.id.btn_1:case R.id.btn_2:case R.id.btn_3:case R.id.btn_4:case R.id.btn_5:case R.id.btn_6:case R.id.btn_7:case R.id.btn_8:case R.id.btn_9:case R.id.btn_point:if(clear_flag){
clear_flag =false;
editText.setText("");
}
editText.setText(input + ((Button)view).getText());break;case R.id.btn_plus:case R.id.btn_minus:case R.id.btn_multply:case R.id.btn_divide:if(clear_flag){
clear_flag =false;
input ="";
editText.setText("");
}
editText.setText(input +" " + ((Button)view).getText() +" ");break;case R.id.btn_clear:
clear_flag =false;
input ="";
editText.setText("");break;case R.id.btn_del:if(clear_flag){
clear_flag =false;
input ="";
editText.setText("");
}elseif(input !=null || !input.equals("")) {
editText.setText(input.substring(0, input.length() -1));
}break;case R.id.btn_equal:
getResult();break;
}
}//运算结果privatevoidgetResult(){
String exp = editText.getText().toString();if(exp==null||exp.equals(""))return;if(!exp.contains(" "))return;if(clear_flag){
clear_flag =false;return;
}
clear_flag =true;double result =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("+")) {
result = d1 + d2;
}elseif (op.equals("-")) {
result = d1 - d2;
}elseif (op.equals("*")) {
result = d1 * d2;
}elseif (op.equals("/")) {if (d2 ==0)
result =0;else
result = d1 / d2;
}if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {int r = (int) result;
editText.setText(r +"");
}else
editText.setText(result +"");
}elseif(!s1.equals("") && s2.equals("")){
editText.setText(exp);
}elseif(s1.equals("") && !s2.equals("")){double d2 = Double.parseDouble(s2);if (op.equals("+")) {
result =0 + d2;
}elseif (op.equals("-")) {
result =0 - d2;
}elseif (op.equals("*")) {
result =0;
}elseif (op.equals("/")) {
result =0;
}if (!s1.contains(".") && !s2.contains(".")) {int r = (int) result;
editText.setText(r +"");
}else
editText.setText(result +"");
}else {
editText.setText("");
}
}
}
总结:一个简单的计算器就这样完成了,现在我们就可以使用自己写的计算器进行一些简单的计算了。