鸿蒙发布了,我来尝尝先,写个hello,world过过瘾。尝试在手表上写一个计算器,结果大致如下,这布局有点不好弄啊,将就着吧。
上面是先按下数字 56, 再按下 * , 再按下 2, 再按下 = 的 结果。
布局代码如下:background_ability_main
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:top_margin="19vp" ohos:orientation="vertical"> <Text ohos:height="30vp" ohos:width="140vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:top_margin="18vp" ohos:left_margin="45vp" ohos:text="0" ohos:id="$+id:txt1" ohos:text_alignment="right" ohos:text_size="20fp"/> <TableLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_content" ohos:width="match_content" ohos:background_element="#87CEEB" ohos:layout_alignment="vertical_center" ohos:row_count="5" ohos:column_count="4" ohos:left_margin="45vp" ohos:padding="2vp"> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="1" ohos:text_alignment="center" ohos:id="$+id:button1" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="2" ohos:id="$+id:button2" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="3" ohos:id="$+id:button3" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="+" ohos:id="$+id:buttonAdd" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="4" ohos:id="$+id:button4" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="5" ohos:id="$+id:button5" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="6" ohos:id="$+id:button6" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="-" ohos:id="$+id:buttonSub" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="7" ohos:id="$+id:button7" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="8" ohos:id="$+id:button8" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="9" ohos:id="$+id:button9" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="*" ohos:id="$+id:buttonMul" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="0" ohos:id="$+id:button0" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="." ohos:id="$+id:buttonPoint" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="=" ohos:id="$+id:buttonEq" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="/" ohos:id="$+id:buttonDiv" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="AC" ohos:id="$+id:buttonAC" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:bg_info.xml" ohos:margin="2vp" ohos:text="" ohos:id="$+id:buttonOP" ohos:text_alignment="center" ohos:text_size="20fp"/> <Button ohos:height="30vp" ohos:width="30vp" ohos:background_element="$graphic:table_text_bg_element.xml" ohos:margin="2vp" ohos:text="" ohos:text_alignment="center" ohos:text_size="20fp"/> </TableLayout> </DirectionalLayout>
bg_info.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> <corners ohos:radius="5vp"/> <stroke ohos:width="1vp" ohos:color="gray"/> <solid ohos:color="gray"/> </shape>
table_text_bg_element.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> <corners ohos:radius="5vp"/> <stroke ohos:width="1vp" ohos:color="gray"/> <solid ohos:color="#00BFFF"/> </shape>
接下来写java代码。这个比布局容易多啦
MainAbilitySlice.java
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); } }