↑大致效果
項目構成:
隨便寫的,用的線性布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top|center" android:orientation="vertical" android:padding="5dp"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:showDividers="middle"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_marginBottom="20dp" android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right|bottom" android:lines="3" android:maxLines="3" android:scrollbars="vertical" android:textColor="#000000" android:textSize="38dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:showDividers="middle"> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:showDividers="middle"> <Button android:id="@+id/btn_cancel" style="@style/btn_cal" android:text="CE" /> <Button android:id="@+id/btn_clear" style="@style/btn_cal" android:text="C" /> <Button android:id="@+id/btn_sqrt" style="@style/btn_cal" android:text="√" /> <Button android:id="@+id/btn_plus" style="@style/btn_cal" android:text="+" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:showDividers="middle"> <Button android:id="@+id/btn_seven" style="@style/btn_cal" android:text="7" /> <Button android:id="@+id/btn_eight" style="@style/btn_cal" android:text="8" /> <Button android:id="@+id/btn_nine" style="@style/btn_cal" android:text="9" /> <Button android:id="@+id/btn_minus" style="@style/btn_cal" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:showDividers="middle"> <Button android:id="@+id/btn_four" style="@style/btn_cal" android:text="4" /> <Button android:id="@+id/btn_five" style="@style/btn_cal" android:text="5" /> <Button android:id="@+id/btn_six" style="@style/btn_cal" android:text="6" /> <Button android:id="@+id/btn_multiply" style="@style/btn_cal" android:text="×" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:showDividers="middle"> <Button android:id="@+id/btn_one" style="@style/btn_cal" android:text="1" /> <Button android:id="@+id/btn_two" style="@style/btn_cal" android:text="2" /> <Button android:id="@+id/btn_three" style="@style/btn_cal" android:text="3" /> <Button android:id="@+id/btn_divide" style="@style/btn_cal" android:text="÷" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:showDividers="middle"> <Button android:id="@+id/btn_zero" style="@style/btn_cal" android:layout_weight="2" android:text="0" /> <Button android:id="@+id/btn_dot" style="@style/btn_cal" android:text="." /> <Button android:id="@+id/btn_equal" style="@style/btn_cal" android:text="=" /> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> </android.support.constraint.ConstraintLayout>
Main_activiity類:
package com.example.myapplication6; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { double sum = 0.0; double temp = 0.0; int op = -1; double step = 1; boolean positive = true; boolean flag = true; TextView tv = null; Button sqrt = null; Button C = null; Button CE = null; Button dot = null; Button num1 = null; Button num2 = null; Button num3 = null; Button num4 = null; Button num5 = null; Button num6 = null; Button num7 = null; Button num8 = null; Button num9 = null; Button num0 = null; Button plus = null; Button subtract = null; Button multiply = null; Button divide = null; Button equal = null; NumUtil numUtil = new NumUtil(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView tv=(TextView) findViewById(R.id.tv_result); num0 = (Button) findViewById(R.id.btn_zero); num0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(temp < 0.00000001 && temp > 0.0) tv.setText("0"); else{ if(flag){ temp *= 10; }else { step /= 10; } tv.setText(numUtil._tv(temp)); } } }); num1 = (Button) findViewById(R.id.btn_one); num1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 1; }else { step /= 10; temp += step * 1.0; } tv.setText(numUtil._tv(temp)); } }); num2 = (Button) findViewById(R.id.btn_two); num2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 2; }else { step /= 10; temp += step * 2; } tv.setText(numUtil._tv(temp)); } }); num3 = (Button) findViewById(R.id.btn_three); num3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 3; }else { step /= 10; temp += step * 3; } tv.setText(numUtil._tv(temp)); } }); num4 = (Button) findViewById(R.id.btn_four); num4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 4; }else { step /= 10; temp += step * 4; } tv.setText(numUtil._tv(temp)); } }); num5 = (Button) findViewById(R.id.btn_five); num5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 5; }else { step /= 10; temp += step * 5; } tv.setText(numUtil._tv(temp)); } }); num6 = (Button) findViewById(R.id.btn_six); num6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 6; }else { step /= 10; temp += step * 6; } tv.setText(numUtil._tv(temp)); } }); num7 = (Button) findViewById(R.id.btn_seven); num7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 7; }else { step /= 10; temp += step * 7; } tv.setText(numUtil._tv(temp)); } }); num8 = (Button) findViewById(R.id.btn_eight); num8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 8; }else { step /= 10; temp += step * 8; } tv.setText(numUtil._tv(temp)); } }); num9 = (Button) findViewById(R.id.btn_nine); num9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ temp *= 10; temp += 9; }else { step /= 10; temp += step * 9; } tv.setText(numUtil._tv(temp)); } }); dot = (Button) findViewById(R.id.btn_dot); dot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(flag){ flag = false; tv.setText(numUtil._tv(temp)+"."); } } }); equal = (Button) findViewById(R.id.btn_equal); equal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(op == -1){ sum = temp; }else if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } tv.setText(numUtil._tv(sum)); positive = true; flag = true; sum = 0.0; temp = 0.0; step = 1.0; op = -1; } }); plus = (Button) findViewById(R.id.btn_plus); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("+"); if(op == -1){ sum = temp; temp = 0.0; }else{ if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } } op = 1; temp = 0.0; positive = true; flag = true; } }); subtract = (Button) findViewById(R.id.btn_minus); subtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("-"); if(op == -1){ sum = temp; temp = 0.0; }else{ if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } } op = 2; temp = 0.0; positive = true; flag = true; } }); multiply = (Button) findViewById(R.id.btn_multiply); multiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("×"); if(op == -1){ sum = temp; temp = 0.0; }else{ if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } } op = 3; temp = 0.0; positive = true; flag = true; } }); divide = (Button) findViewById(R.id.btn_divide); divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("÷"); if(op == -1){ sum = temp; temp = 0.0; }else{ if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } } op = 4; temp = 0.0; positive = true; flag = true; } }); C = (Button) findViewById(R.id.btn_cancel); C.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText(" "); positive = true; flag = true; temp = 0.0; } }); CE = (Button) findViewById(R.id.btn_clear); CE.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText(" "); positive = true; flag = true; sum = 0.0; temp = 0.0; step = 1.0; op = -1; } }); sqrt = (Button) findViewById(R.id.btn_sqrt); sqrt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(op == -1){ sum = temp; }else if(op == 1){ sum += temp; }else if(op == 2){ sum -= temp; }else if(op == 3){ sum *= temp; }else if(op == 4){ sum /= temp; } sum = Math.sqrt(sum); tv.setText(numUtil._tv(sum)); positive = true; flag = true; sum = 0.0; temp = 0.0; step = 1.0; op = -1; } }); } }
NumUtil工具類:
package com.example.myapplication6; public class NumUtil { public double toDouble(String str){ return 0.0; } public String _tv(double x){ double t = x - (int) x; if(t < 0) t = -t; if(t < 0.00000001) return String.valueOf((int)x); else{ String temp = String.format("%.7f", x); for(int i = temp.length() -1; i >= 0; i--){ if(temp.substring(i,i+1).equals("0")){ continue; }else{ if(i + 1 < temp.length()){ temp = temp.substring(0,i+1); } break; } } return temp; } } }
按鈕的垃圾設定
<style name="btn_cal"> <item name="android:layout_marginLeft">3dp</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:background">#EEA9B8</item> <item name="android:textSize">30px</item> </style>
不太好弄的地方就是對於小數點后的處理了,剩下的隨便寫寫就完事了