首页
社区
课程
招聘
[原创]鸿蒙不出,谁与争锋,是时候发布我的第一个鸿蒙APP了
发表于: 2021-6-5 10:49 24239

[原创]鸿蒙不出,谁与争锋,是时候发布我的第一个鸿蒙APP了

2021-6-5 10:49
24239

鸿蒙发布了,我来尝尝先,写个hello,world过过瘾。尝试在手表上写一个计算器,结果大致如下,这布局有点不好弄啊,将就着吧。
图片描述
上面是先按下数字 56, 再按下 * , 再按下 2, 再按下 = 的 结果。

 

布局代码如下:background_ability_main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?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

1
2
3
4
5
6
7
8
9
10
11
<?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

1
2
3
4
5
6
7
8
9
10
11
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);
    }
}

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 7
支持
分享
最新回复 (34)
雪    币: 1785
活跃值: (3575)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
2
看到看雪多了一个鸿蒙板块,过来凑凑热闹,哈哈
2021-6-5 10:51
0
雪    币: 1385
活跃值: (5609)
能力值: ( LV3,RANK:25 )
在线值:
发帖
回帖
粉丝
3
不错不错。
2021-6-5 11:07
0
雪    币: 248
活跃值: (3789)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
好,鸿蒙板块第一个hello,world
2021-6-5 11:45
0
雪    币: 26399
活跃值: (63262)
能力值: (RANK:135 )
在线值:
发帖
回帖
粉丝
5
hello bro
2021-6-5 11:55
1
雪    币: 5567
活跃值: (2749)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
hello,鸿蒙
2021-6-5 15:28
0
雪    币: 3072
活跃值: (20)
能力值: ( LV1,RANK:40 )
在线值:
发帖
回帖
粉丝
7
hello,HarmonyOS
楼主放一个编译的程序上来?看看用啥工具可以反编译
2021-6-5 16:19
2
雪    币: 4222
活跃值: (1603)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
8
这要捧场一下!
2021-6-5 18:32
0
雪    币: 311
活跃值: (336)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
hh,看雪这个Harmony的版块就加的很及时!
2021-6-5 20:40
0
雪    币: 181
活跃值: (621)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
中国华为鸿蒙,牛皮,666,我勒宝贝儿
2021-6-6 09:30
0
雪    币: 2089
活跃值: (3933)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
不错哦
2021-6-6 15:16
0
雪    币: 3496
活跃值: (749)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
这个用啥编译?android studio?
2021-6-6 20:39
0
雪    币: 2089
活跃值: (3933)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
13
kxzpy 这个用啥编译?android studio?
DevEco Studio
2021-6-6 20:54
0
雪    币: 775
活跃值: (2292)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
14
支持支持
2021-6-6 23:20
0
雪    币: 40
活跃值: (675)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
支持鸿蒙
2021-6-7 10:22
0
雪    币: 10603
活跃值: (2294)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
计算器界面可以做成原型  做大使用面积,异型按键
2021-6-7 11:16
0
雪    币: 182
活跃值: (523)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
支持大佬
2021-6-8 08:41
0
雪    币: 864
活跃值: (5124)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
楼主牛逼
2021-6-8 09:13
0
雪    币: 228
活跃值: (19)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
19
hello,HarmonyOS
2021-6-8 12:53
0
雪    币: 79
活跃值: (40)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
20
鸿蒙6666
2021-6-11 11:46
0
雪    币: 121
活跃值: (825)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
21

6666666
2021-6-11 21:45
0
雪    币: 1802
活跃值: (4000)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
22
好,代码清晰优美
2021-6-11 23:11
0
雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
23
会写代码的就是牛
2021-6-14 08:37
0
雪    币: 218
活跃值: (381)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
66666666666666
2021-6-18 16:47
0
雪    币: 88
活跃值: (109)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
25
请问鸿蒙支持C#吗?.net core能不能在鸿蒙系统上跑?
2021-6-18 16:59
0
游客
登录 | 注册 方可回帖
返回
//