鸿蒙期末大作业
1、简介
使用开发工具DevEco studio,用java语言编写一个简单的应用——计算机,本应用能够进行简单的加、减、乘、除混合运算。
2、成果展示
3、
设计计算器各个按钮
private void initData() {
if (map == null)
map = new HashMap<Component, String>();
map.put(mAddBtn, "+");
map.put(mMultiplyBtn, "*");
map.put(mDividebtn, "/");
map.put(mSubBtn, "-");
map.put(mZeroButton, "0");
map.put(mOnebtn, "1");
map.put(mTwoBtn, "2");
map.put(mThreeBtn, "3");
map.put(mFourBtn, "4");
map.put(mFiveBtn, "5");
map.put(mSixBtn, "6");
map.put(mSevenBtn, "7");
map.put(mEightBtn, "8");
map.put(mNineBtn, "9");
map.put(mPointtn, ".");
map.put(mEqualBtn, "=");
mInputList = new ArrayList<>();
mShowResultTv.setText("");
clearAllScreen();
}
部分代码
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
private TextField mShowResultTv;
private Text mShowInputTv;
private Button mCBtn;
private Button mDelBtn;
private Button mAddBtn;
private Button mSubBtn;
private Button mMultiplyBtn;
private Button mDividebtn;
private Button mZeroButton;
private Button mOnebtn;
private Button mTwoBtn;
private Button mThreeBtn;
private Button mFourBtn;
private Button mFiveBtn;
private Button mSixBtn;
private Button mSevenBtn;
private Button mEightBtn;
private Button mNineBtn;
private Button mPointtn;
private Button mEqualBtn;
private HashMap<Component, String> map;
private List<InputItem> mInputList;
private int mLastInputstatus = INPUT_NUMBER;
public static final int INPUT_NUMBER = 1;
public static final int INPUT_POINT = 0;
public static final int INPUT_OPERATOR = -1;
public static final int END = -2;
public static final int ERROR = -3;
public static final int SHOW_RESULT_DATA = 1;
public static final String nan = "NaN";
public static final String infinite = "??";
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
}
@Override
public void onActive() {
super.onActive();
initView();
initData();
}
private void initView() {
mShowResultTv = this.findComponentById(ResourceTable.Id_show_result_tv);
mShowInputTv = this.findComponentById(ResourceTable.Id_show_input_tv);
mCBtn = this.findComponentById(ResourceTable.Id_c_btn);
mDelBtn = this.findComponentById(ResourceTable.Id_del_btn);
mAddBtn = this.findComponentById(ResourceTable.Id_add_btn);
mMultiplyBtn = (Button) this.findComponentById(ResourceTable.Id_multiply_btn);
mDividebtn = (Button) this.findComponentById(ResourceTable.Id_divide_btn);
mZeroButton = (Button) this.findComponentById(ResourceTable.Id_zero_btn);
mOnebtn = (Button) this.findComponentById(ResourceTable.Id_one_btn);
mTwoBtn = (Button) this.findComponentById(ResourceTable.Id_two_btn);
mThreeBtn = (Button) this.findComponentById(ResourceTable.Id_three_btn);
mFourBtn = (Button) this.findComponentById(ResourceTable.Id_four_btn);
mFiveBtn = (Button) this.findComponentById(ResourceTable.Id_five_btn);
mSixBtn = (Button) this.findComponentById(ResourceTable.Id_six_btn);
mSevenBtn = (Button) this.findComponentById(ResourceTable.Id_seven_btn);
mEightBtn = (Button) this.findComponentById(ResourceTable.Id_eight_btn);
mNineBtn = (Button) this.findComponentById(ResourceTable.Id_nine_btn);
mPointtn = (Button) this.findComponentById(ResourceTable.Id_point_btn);
mEqualBtn = (Button) this.findComponentById(ResourceTable.Id_equal_btn);
mSubBtn = (Button) this.findComponentById(ResourceTable.Id_sub_btn);
setClickedListener();
}
private void initData() {
if (map == null)
map = new HashMap<Component, String>();
map.put(mAddBtn, "+");
map.put(mMultiplyBtn, "*");
map.put(mDividebtn, "/");
map.put(mSubBtn, "-");
map.put(mZeroButton, "0");
map.put(mOnebtn, "1");
map.put(mTwoBtn, "2");
map.put(mThreeBtn, "3");
map.put(mFourBtn, "4");
map.put(mFiveBtn, "5");
map.put(mSixBtn, "6");
map.put(mSevenBtn, "7");
map.put(mEightBtn, "8");
map.put(mNineBtn, "9");
map.put(mPointtn, ".");
map.put(mEqualBtn, "=");
mInputList = new ArrayList<>();
mShowResultTv.setText("");
clearAllScreen();
}
private void setClickedListener() {
mCBtn.setClickedListener(this);
mDelBtn.setClickedListener(this);
mAddBtn.setClickedListener(this);
mMultiplyBtn.setClickedListener(this);
mDividebtn.setClickedListener(this);
mSubBtn.setClickedListener(this);
mZeroButton.setClickedListener(this);
mOnebtn.setClickedListener(this);
mTwoBtn.setClickedListener(this);
mThreeBtn.setClickedListener(this);
mFourBtn.setClickedListener(this);
mFiveBtn.setClickedListener(this);
mSixBtn.setClickedListener(this);
mSevenBtn.setClickedListener(this);
mEightBtn.setClickedListener(this);
mNineBtn.setClickedListener(this);
mPointtn.setClickedListener(this);
mEqualBtn.setClickedListener(this);
}
@Override
public void onClick(Component arg0) {
switch (arg0.getId()) {
case ResourceTable.Id_c_btn:
clearAllScreen();
break;
case ResourceTable.Id_del_btn:
back();
break;
case ResourceTable.Id_point_btn:
inputPoint(arg0);
break;
case ResourceTable.Id_equal_btn:
operator();
break;
case ResourceTable.Id_add_btn:
case ResourceTable.Id_sub_btn:
case ResourceTable.Id_multiply_btn:
case ResourceTable.Id_divide_btn:
inputOperator(arg0);
break;
default:
inputNumber(arg0);
break;
}
}
private void operator() {
if (mLastInputstatus == END || mLastInputstatus == ERROR || mLastInputstatus == INPUT_OPERATOR || mInputList.size() == 1) {
return;
}
mShowResultTv.setText("");
startAnim();
findHighOperator(0);
if (mLastInputstatus != ERROR) {
findLowOperator(0);
}
mShowResultTv.setText(mShowInputTv.getText());
String str = mInputList.get(0).getInput();
BigDecimal b = new BigDecimal(str);
double d = b.setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
String res = d + "";
if (res.endsWith(".0")) {
res = res.substring(0, res.indexOf("."));
}
mShowInputTv.setText(res);
clearScreen(mInputList.get(0));
}
private void startAnim() {
mShowInputTv.setText(mShowInputTv.getText() + "=");
}
private void inputPoint(Component view) {
if (mLastInputstatus == INPUT_POINT) {
return;
}
if (mLastInputstatus == END || mLastInputstatus == ERROR) {
clearInputScreen();
}
String key = map.get(view);
String input = mShowInputTv.getText().toString();
if (mLastInputstatus == INPUT_OPERATOR) {
input = input + "0";
}
mShowInputTv.setText(input + key);
addInputList(INPUT_POINT, key);
}
/**
* @param view
*/
private void inputNumber(Component view) {
if (mLastInputstatus == END || mLastInputstatus == ERROR) {
clearInputScreen();
}
String key = map.get(view);
if ("0".equals(mShowInputTv.getText().toString())) {
mShowInputTv.setText(key);
} else {
mShowInputTv.setText(mShowInputTv.getText() + key);
}
addInputList(INPUT_NUMBER, key);
}
private void inputOperator(Component view) {
if (mLastInputstatus == INPUT_OPERATOR || mLastInputstatus == ERROR) {
return;
}
if (mLastInputstatus == END) {
mLastInputstatus = INPUT_NUMBER;
}
String key = map.get(view);
if ("0".equals(mShowInputTv.getText().toString())) {
mShowInputTv.setText("0" + key);
mInputList.set(0, new InputItem("0", InputType.INT_TYPE));
} else {
mShowInputTv.setText(mShowInputTv.getText() + key);
}
addInputList(INPUT_OPERATOR, key);
}
/**
*
*/
private void back() {
if (mLastInputstatus == ERROR) {
clearInputScreen();
}
String str = mShowInputTv.getText().toString();
if (str.length() != 1) {
mShowInputTv.setText(str.substring(0, str.length() - 1));
backList();
} else {
mShowInputTv.setText("0");
clearScreen(new InputItem("", InputType.INT_TYPE));
}
}
标签:江鸟,map,鸿蒙,Button,private,ResourceTable,计算器,put,Id
From: https://blog.51cto.com/u_16413987/8741720