Commit 48a3a245 authored by zhang_z's avatar zhang_z

计算器基本完成;

parent 1708d114
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xingdata.zzdpos"> package="com.xingdata.zzdpos">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.SEND_SMS" />
<application <application
android:name=".App" android:name=".App"
...@@ -19,18 +19,15 @@ ...@@ -19,18 +19,15 @@
android:configChanges="keyboard|orientation|screenSize|keyboardHidden" android:configChanges="keyboard|orientation|screenSize|keyboardHidden"
android:label="@string/main_title" android:label="@string/main_title"
android:launchMode="singleTask" android:launchMode="singleTask"
android:windowSoftInputMode="adjustUnspecified|stateHidden" > android:windowSoftInputMode="adjustUnspecified|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity> </activity>
<activity <activity
android:name=".ui.splash.SplashActivity" android:name=".ui.splash.SplashActivity"
android:configChanges="keyboard|orientation|screenSize|keyboardHidden" android:configChanges="keyboard|orientation|screenSize|keyboardHidden"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
</activity> </activity>
<activity <activity
...@@ -49,7 +46,13 @@ ...@@ -49,7 +46,13 @@
<activity <activity
android:name=".ui.payment.PaymentActivity" android:name=".ui.payment.PaymentActivity"
android:configChanges="keyboard|orientation|screenSize|keyboardHidden" android:configChanges="keyboard|orientation|screenSize|keyboardHidden"
android:windowSoftInputMode="adjustUnspecified|stateHidden" /> android:windowSoftInputMode="adjustUnspecified|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -23,6 +23,8 @@ public class PaymentFragment extends BaseFragment<PaymentPresenter, FragmentPaym ...@@ -23,6 +23,8 @@ public class PaymentFragment extends BaseFragment<PaymentPresenter, FragmentPaym
mCalculatorView.setmOnResultChangeListener((exp, result) -> { mCalculatorView.setmOnResultChangeListener((exp, result) -> {
LogUtils.e(exp + "\n" + result); LogUtils.e(exp + "\n" + result);
mViewBinding.tvExp.setText(exp);
mViewBinding.tvResult.setText(ConvertUtil.fenToYuan(result));
}); });
mCalculatorView.setmOnSettleClickListener(result -> { mCalculatorView.setmOnSettleClickListener(result -> {
LogUtils.e(ConvertUtil.fenToYuan(result)); LogUtils.e(ConvertUtil.fenToYuan(result));
......
...@@ -4,6 +4,7 @@ package com.xingdata.zzdpos.ui.payment.view; ...@@ -4,6 +4,7 @@ package com.xingdata.zzdpos.ui.payment.view;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.blankj.utilcode.util.LogUtils;
import com.xingdata.zzdpos.R; import com.xingdata.zzdpos.R;
import com.xingdata.zzdpos.base.BaseFragment; import com.xingdata.zzdpos.base.BaseFragment;
import com.xingdata.zzdpos.databinding.ViewCalculatorBinding; import com.xingdata.zzdpos.databinding.ViewCalculatorBinding;
...@@ -18,6 +19,7 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -18,6 +19,7 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
private String mExp; private String mExp;
private String mValue; private String mValue;
private String mLast;
private OnResultChangeListener mOnResultChangeListener; private OnResultChangeListener mOnResultChangeListener;
private onSettleClickListener mOnSettleClickListener; private onSettleClickListener mOnSettleClickListener;
...@@ -26,6 +28,7 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -26,6 +28,7 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
public CalculatorView() { public CalculatorView() {
mExp = ""; mExp = "";
mValue = ""; mValue = "";
mLast = "";
} }
public interface OnResultChangeListener { public interface OnResultChangeListener {
...@@ -55,24 +58,28 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -55,24 +58,28 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
settle(); settle();
break; break;
default: default:
LogUtils.e("mExp : " + mExp + "\nmValue : " + mValue + "\nmLast" + mLast);
if (view instanceof TextView) { if (view instanceof TextView) {
String str = ((TextView) view).getText().toString(); String str = ((TextView) view).getText().toString();
if (mExp.length() == 0 && str.matches("[+\\-*/]")) return;
if (mExp.length() == 0 && str.equals(".")) mExp += "0";
if (mValue.length() > 0 && str.matches("[+\\-*/]")) { String[] mExps = mExp.split("[+\\-×÷]");
if (mExps.length > 1) mValue = mExps[mExps.length - 1];
else mValue = mExp;
if (mLast.matches("[+\\-×÷.]") && str.matches("[+\\-×÷.]")) return;
if (mValue.length() > 0 && str.matches("[+\\-×÷]")) {
mValue = ""; mValue = "";
} else { } else {
if (mValue.contains(".") && str.matches("[.]")) return; if (mValue.length() == 0 && str.matches("[+\\-×÷]")) return;
if (mValue.contains(".") && str.matches("[+\\-×÷.]")) return;
mValue += str; if (mValue.split("[.]").length > 1 && mValue.split("[.]")[1].length() > 2)
String[] _values = mValue.split("[.]"); return;
if (_values.length > 1) { if (mValue.length() == 0 && str.equals(".")) mExp += "0";
if (_values[1].length() > 2) return;
}
} }
mLast = str;
mExp += str; mExp += str;
} }
break; break;
...@@ -88,7 +95,10 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -88,7 +95,10 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
* 删除 * 删除
*/ */
private void delete() { private void delete() {
mExp = mExp.substring(0, mExp.length() - 1); if (mExp.length() > 0) {
mExp = mExp.substring(0, mExp.length() - 1);
mLast = mExp.substring(mExp.length() - 1, mExp.length());
}
} }
/** /**
...@@ -124,12 +134,11 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -124,12 +134,11 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
.replaceAll("×", "*").replaceAll("÷", "/"); .replaceAll("×", "*").replaceAll("÷", "/");
String minExp = "^((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))[+\\-*/]((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))$"; String minExp = "^((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))[+\\-*/]((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))$";
if (exp.matches(minExp)) { if (exp.matches(minExp)) {
String result = calculate(exp); return calculate(exp);
return Double.parseDouble(result) >= 0 ? result : "[" + result + "]";
} else if (exp.matches("[0-9.]+")) { } else if (exp.matches("[0-9.]+")) {
return exp; return exp;
} else if (exp.length() == 0) { } else if (exp.length() == 0) {
return "0"; return "0.0";
} }
String noParentheses = "^[^()]+$"; String noParentheses = "^[^()]+$";
String priorOperatorExp = "(((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))[*/]((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?])))"; String priorOperatorExp = "(((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?]))[*/]((\\d+(\\.\\d+)?)|(\\[-\\d+(\\.\\d+)?])))";
...@@ -143,7 +152,6 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato ...@@ -143,7 +152,6 @@ public class CalculatorView extends BaseFragment<PaymentPresenter, ViewCalculato
} else { } else {
patt = Pattern.compile(operatorExp); patt = Pattern.compile(operatorExp);
mat = patt.matcher(exp); mat = patt.matcher(exp);
if (mat.find()) { if (mat.find()) {
String tempMinExp = mat.group(); String tempMinExp = mat.group();
exp = exp.replaceFirst(operatorExp, parseExp(tempMinExp)); exp = exp.replaceFirst(operatorExp, parseExp(tempMinExp));
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
...@@ -13,6 +14,20 @@ ...@@ -13,6 +14,20 @@
android:background="@color/store_product_bg" android:background="@color/store_product_bg"
android:orientation="vertical"> android:orientation="vertical">
<TextView
android:id="@+id/tv_exp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:textColor="@color/white_caocao" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:textColor="@color/white_caocao" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment