package com.example.myapplication1.
slice
;
import
com.example.myapplication1.ResourceTable;
import
ohos.aafwk.ability.AbilitySlice;
import
ohos.aafwk.content.Intent;
import
ohos.agp.components.Button;
import
ohos.agp.components.Component;
import
ohos.agp.components.Text;
import
java.math.BigDecimal;
import
java.math.BigInteger;
public
class
MainAbilitySlice extends AbilitySlice {
Text txt1;
String num;
Boolean isNew
=
false;
@Override
public void onStart(Intent intent) {
super
.onStart(intent);
super
.setUIContent(ResourceTable.Layout_ability_main);
txt1
=
(Text) findComponentById(ResourceTable.Id_txt1);
Button button1
=
(Button) findComponentById(ResourceTable.Id_button1);
Button button2
=
(Button) findComponentById(ResourceTable.Id_button2);
Button button3
=
(Button) findComponentById(ResourceTable.Id_button3);
Button button4
=
(Button) findComponentById(ResourceTable.Id_button4);
Button button5
=
(Button) findComponentById(ResourceTable.Id_button5);
Button button6
=
(Button) findComponentById(ResourceTable.Id_button6);
Button button7
=
(Button) findComponentById(ResourceTable.Id_button7);
Button button8
=
(Button) findComponentById(ResourceTable.Id_button8);
Button button9
=
(Button) findComponentById(ResourceTable.Id_button9);
Button button0
=
(Button) findComponentById(ResourceTable.Id_button0);
Button btnPoint
=
(Button) findComponentById(ResourceTable.Id_buttonPoint);
button1.setClickedListener(new ClickedListenerNumber());
button2.setClickedListener(new ClickedListenerNumber());
button3.setClickedListener(new ClickedListenerNumber());
button4.setClickedListener(new ClickedListenerNumber());
button5.setClickedListener(new ClickedListenerNumber());
button6.setClickedListener(new ClickedListenerNumber());
button7.setClickedListener(new ClickedListenerNumber());
button8.setClickedListener(new ClickedListenerNumber());
button9.setClickedListener(new ClickedListenerNumber());
button0.setClickedListener(new ClickedListenerNumber());
btnPoint.setClickedListener(new ClickedListenerPoint());
Button btnAdd
=
(Button)findComponentById(ResourceTable.Id_buttonAdd);
Button btnSub
=
(Button)findComponentById(ResourceTable.Id_buttonSub);
Button btnMul
=
(Button)findComponentById(ResourceTable.Id_buttonMul);
Button btnDiv
=
(Button)findComponentById(ResourceTable.Id_buttonDiv);
btnAdd.setClickedListener(new ClickedListenerOP());
btnSub.setClickedListener(new ClickedListenerOP());
btnMul.setClickedListener(new ClickedListenerOP());
btnDiv.setClickedListener(new ClickedListenerOP());
Button btnAC
=
(Button)findComponentById(ResourceTable.Id_buttonAC);
btnAC.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
num
=
"";
isNew
=
false;
txt1.setText(
"0"
);
Button btnOP
=
(Button)findComponentById(ResourceTable.Id_buttonOP);
btnOP.setText("");
}
});
Button btnEQ
=
(Button)findComponentById(ResourceTable.Id_buttonEq);
btnEQ.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
BigDecimal d1
=
new BigDecimal(num);
BigDecimal d2
=
new BigDecimal(txt1.getText());
BigDecimal res;
Button btnOP
=
(Button)findComponentById(ResourceTable.Id_buttonOP);
switch (btnOP.getText()) {
case
"+"
: res
=
d1.add(d2);
break
;
case
"-"
: res
=
d1.subtract(d2);
break
;
case
"*"
: res
=
d1.multiply(d2);
break
;
case
"/"
: {
try
{ res
=
d1.divide(d2); } catch (Exception e) {res
=
BigDecimal.valueOf(
0
); }
break
;
}
default: res
=
BigDecimal.valueOf(
0
);
};
txt1.setText(res.stripTrailingZeros().toPlainString());
}
});
}
class
ClickedListenerNumber implements Component.ClickedListener {
@Override
public void onClick(Component component) {
Button btn
=
(Button)component;
if
(txt1.getText().equals(
"0"
) || isNew
=
=
true) {
txt1.setText( btn.getText() );
}
else
{
txt1.setText( txt1.getText()
+
btn.getText() );
}
isNew
=
false;
}
}
class
ClickedListenerPoint implements Component.ClickedListener {
@Override
public void onClick(Component component) {
if
( txt1.getText().indexOf(
'.'
)
=
=
-
1
)
txt1.setText( txt1.getText()
+
'.'
);
}
}
class
ClickedListenerOP implements Component.ClickedListener {
@Override
public void onClick(Component component) {
Button btn
=
(Button)component;
Button btnOP
=
(Button)findComponentById(ResourceTable.Id_buttonOP);
btnOP.setText(btn.getText());
num
=
txt1.getText();
isNew
=
true;
}
}
@Override
public void onActive() {
super
.onActive();
}
@Override
public void onForeground(Intent intent) {
super
.onForeground(intent);
}
}