零基础学习鸿蒙App开发 (第一天)

最近发几条质疑华鸿蒙系统的微头条,受了很多网友的嘲笑,我准备零基础自学App开发,我自己是运维,主要工作是维护Linux和Windows 服务器,同时兼桌面运维和网络。

废话不说,这是今天下午的成果。

安装开发工具

创建项目

我不懂js ,只能选择java作为开发语言

自动下载gradle

安装SDK

配置模拟器,需要开发者帐户,本人早有开发者账号,这里不用申请 了。

运行默认的空项目

查看开发文档,开发一个两位数相加的计算器

逻辑非常简单,xml里定义布局和几个组件

1 x

2 y

3 result

4 button

在MainAbility.java中处理相关逻辑,即获取 x y的值 相加并赋值给result

耗时3个小时完工

代码如下: ability_main.xml

<?xml version='1.0' encoding='utf-8'?><DirectionalLayout xmlns:ohos='http://schemas.huawei.com/res/ohos' ohos:height='match_parent' ohos:width='match_parent' ohos:orientation='vertical'> <Text ohos:id='$+id:demo' ohos:height='match_content' ohos:width='match_content' ohos:background_element='$graphic:background_ability_main' ohos:text_color='black' ohos:layout_alignment='horizontal_center' ohos:text='加法计算器' ohos:text_size='150' ohos:selection_color='green' /> <TextField ohos:id='$+id:x' ohos:height='match_content' ohos:width='match_content' ohos:left_padding='20vp' ohos:right_padding='20vp' ohos:text_size='100' ohos:layout_alignment='horizontal_center' ohos:hint='输入第一个数字' ohos:text_input_type='pattern_number' /> <Text ohos:id='$+id:text_add' ohos:height='match_content' ohos:width='match_content' ohos:background_element='$graphic:background_ability_main' ohos:text_color='red' ohos:layout_alignment='horizontal_center' ohos:text='' ohos:text_size='100' ohos:selection_color='green' ohos:hint='+' /> <TextField ohos:id='$+id:y' ohos:layout_alignment='horizontal_center' ohos:height='match_content' ohos:width='match_content' ohos:left_padding='20vp' ohos:right_padding='20vp' ohos:text_size='100' ohos:hint='输入第二个数字' ohos:text_input_type='pattern_number' /> <Text ohos:id='$+id:text_eq' ohos:height='match_content' ohos:width='match_content' ohos:background_element='$graphic:background_ability_main' ohos:text_color='red' ohos:layout_alignment='horizontal_center' ohos:text='' ohos:text_size='100' ohos:selection_color='green' ohos:hint='=' /> <TextField ohos:id='$+id:result' ohos:height='match_content' ohos:width='match_content' ohos:background_element='$graphic:background_ability_main' ohos:text_color='red' ohos:layout_alignment='horizontal_center' ohos:text='' ohos:text_size='150' ohos:selection_color='green' ohos:hint='这里显示计算结果' /> <Button ohos:layout_alignment='horizontal_center' ohos:height='match_content' ohos:width='match_content' ohos:id='$+id:add' ohos:text='计算' ohos:text_size='100' /></DirectionalLayout>

MainAbility.java

package com.example.demo;import com.example.demo.slice.MainAbilitySlice;import ohos.aafwk.ability.Ability;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.Text;import ohos.agp.components.TextField;public class MainAbility extends Ability {    @Override    public void onStart(Intent intent) {        super.onStart(intent);        super.setUIContent(ResourceTable.Layout_ability_main);        super.setMainRoute(MainAbilitySlice.class.getName());        Button button_add = (Button) findComponentById(ResourceTable.Id_add);        button_add.setClickedListener(new Component.ClickedListener(){            @Override            public void onClick(Component component) {                TextField text_x = (TextField) findComponentById(ResourceTable.Id_x);                int x = Integer.parseInt(text_x.getText());                TextField text_y = (TextField) findComponentById(ResourceTable.Id_y);                int y = Integer.parseInt(text_y.getText());                int result = x + y;                TextField text_result = (TextField) findComponentById(ResourceTable.Id_result);                text_result.setText(String.valueOf(result));                System.out.println(result);            }        });    }    @Override    public void onActive() {        super.onActive();    }    @Override    public void onForeground(Intent intent) {        super.onForeground(intent);    }}

明天做好签名准备打包hap

(0)

相关推荐