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

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

2021-6-5 10:49
23039

鸿蒙发布了,我来尝尝先,写个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);
    }
}

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞6
打赏
分享
最新回复 (34)
雪    币: 1025
活跃值: (2830)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
中国小孩大人 2021-6-5 10:51
2
0
看到看雪多了一个鸿蒙板块,过来凑凑热闹,哈哈
雪    币: 1366
活跃值: (5584)
能力值: ( LV3,RANK:25 )
在线值:
发帖
回帖
粉丝
supperlitt 2021-6-5 11:07
3
0
不错不错。
雪    币: 248
活跃值: (3779)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
luskyc 2021-6-5 11:45
4
0
好,鸿蒙板块第一个hello,world
雪    币: 17792
活跃值: (60003)
能力值: (RANK:125 )
在线值:
发帖
回帖
粉丝
Editor 2021-6-5 11:55
5
1
hello bro
雪    币: 4481
活跃值: (1784)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
看雪vip用户 2021-6-5 15:28
6
0
hello,鸿蒙
雪    币: 3072
活跃值: (20)
能力值: ( LV1,RANK:40 )
在线值:
发帖
回帖
粉丝
0x99er 2021-6-5 16:19
7
2
hello,HarmonyOS
楼主放一个编译的程序上来?看看用啥工具可以反编译
雪    币: 1992
活跃值: (493)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
fdark 2021-6-5 18:32
8
0
这要捧场一下!
雪    币: 311
活跃值: (336)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
SmallDolphin 2021-6-5 20:40
9
0
hh,看雪这个Harmony的版块就加的很及时!
雪    币: 182
活跃值: (576)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
柒雪天尚 2021-6-6 09:30
10
0
中国华为鸿蒙,牛皮,666,我勒宝贝儿
雪    币: 2063
活跃值: (3823)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lhxdiao 2021-6-6 15:16
11
0
不错哦
雪    币: 3496
活跃值: (749)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kxzpy 2021-6-6 20:39
12
0
这个用啥编译?android studio?
雪    币: 2063
活跃值: (3823)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lhxdiao 2021-6-6 20:54
13
0
kxzpy 这个用啥编译?android studio?
DevEco Studio
雪    币: 775
活跃值: (2292)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
AperOdry 2021-6-6 23:20
14
0
支持支持
雪    币: 239
活跃值: (650)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
只是来打酱油 2021-6-7 10:22
15
0
支持鸿蒙
雪    币: 10089
活跃值: (1729)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
wzmooo 2021-6-7 11:16
16
0
计算器界面可以做成原型  做大使用面积,异型按键
雪    币: 182
活跃值: (503)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Aspark-zm 2021-6-8 08:41
17
0
支持大佬
雪    币: 839
活跃值: (5004)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
suuuuu 2021-6-8 09:13
18
0
楼主牛逼
雪    币: 228
活跃值: (19)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
系统正在维护 2021-6-8 12:53
19
0
hello,HarmonyOS
雪    币: 79
活跃值: (40)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
韩子 2021-6-11 11:46
20
0
鸿蒙6666
雪    币: 112
活跃值: (595)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
草mao 2021-6-11 21:45
21
0

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