@@ -5,7 +5,7 @@ import androidx.compose.animation.AnimatedVisibility
55import androidx.compose.foundation.background
66import androidx.compose.foundation.clickable
77import androidx.compose.foundation.interaction.MutableInteractionSource
8- import androidx.compose.foundation.interaction.collectIsPressedAsState
8+ import androidx.compose.foundation.interaction.collectIsFocusedAsState
99import androidx.compose.foundation.layout.*
1010import androidx.compose.foundation.shape.RoundedCornerShape
1111import androidx.compose.material.icons.Icons
@@ -77,13 +77,13 @@ fun DefaultView() {
7777 val interactionSource: MutableInteractionSource by remember {
7878 mutableStateOf(MutableInteractionSource ())
7979 }
80- val textFieldPressed = interactionSource.collectIsPressedAsState ()
80+ val textFieldPressed = interactionSource.collectIsFocusedAsState ()
8181
8282 // 输入框 State 管理
8383 val state =
8484 rememberCalculatorTextFieldState(
8585 interactionSource = interactionSource,
86- cursorHide = { textFieldPressed.value },
86+ cursorHide = { ! textFieldPressed.value },
8787 onValueChange = { updateBracketsCounts(it) },
8888 onDone = { startCalculatingEquations(it) }
8989 )
@@ -261,19 +261,32 @@ val onClick: String.(String, CalculatorTextFieldState) -> Unit = { text, state -
261261 isOperator() -> {
262262 state.apply {
263263 when (true ) {
264+ // 如果最后一个是数字直接添加,其他分支走符号处理
264265 isOperatorPercentage() -> {
265- if (last.isNumber())
266- add(text)
266+ when (true ) {
267+ last.isNumber() -> add(text)
268+ (last.isOperatorMIN() && lastSecond.isOperator()) -> {
269+ deleteLast()
270+ add(text,true )
271+ }
272+ last.isOperator() -> {
273+ if (lastSecond.isOperatorPercentage())
274+ deleteLast()
275+ add(text, true )
276+ }
277+ else -> {}
278+ }
267279 }
268280 // 对于需要输入负数的情况
269281 (isOperatorMIN() && last.isOperator()) -> {
270282 add(text, lastSecond.isOperator())
271283 }
272284 // 替换 --
273- (last.isOperatorMIN() && lastSecond.isOperator()) -> {
285+ (last.isOperatorMIN() && lastSecond.isOperator() && ! lastSecond.isOperatorPercentage() ) -> {
274286 deleteLast()
275287 add(text, true )
276288 }
289+ // 运算符号处理需要排除百分号
277290 (last.isOperator() && ! last.isOperatorPercentage()) -> add(text, true )
278291 else -> add(text)
279292 }
0 commit comments