鴻蒙初次demo編寫
資格申請
前往華為開發(fā)者聯(lián)盟首頁點(diǎn)擊管理中心(如果沒有賬號(hào)需要注冊及實(shí)名認(rèn)證)
選擇HarmonyOS開發(fā)者授權(quán)如圖
在其中可下載IDE:DevEcoStudio账阻,然后安裝即可
編譯器可創(chuàng)建java項(xiàng)目也可創(chuàng)建js項(xiàng)目,我創(chuàng)建的是Java項(xiàng)目
項(xiàng)目
項(xiàng)目文件夾
Java部分基本與安卓相同
resources中需要注意
graphic相當(dāng)于安卓的drawable
media相當(dāng)于mipmap
代碼部分
Android創(chuàng)建新項(xiàng)目默認(rèn)創(chuàng)建的是MainActivity
鴻蒙默認(rèn)創(chuàng)建的是MainAbility
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
}
1铣减、Ability就相當(dāng)于安卓的Activity
2祟滴、生命周期也是不同的
3废登、并沒有在此Ability中關(guān)聯(lián)布局文件琳钉,而是引用了一個(gè)名為MainAbilitySlice的類
Slice類
slice類的默認(rèn)代碼
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
可以看出此類和我們的activity很像其中的setUIContent方法關(guān)聯(lián)了xml文件,應(yīng)用方法為ResourceTable這個(gè)方法個(gè)人認(rèn)為相當(dāng)于安卓中的R
并且生命周期和安卓名稱完全不同
xml文件
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:height="match_parent"
ohos:width="match_parent"
ohos:text="HelloWord"
ohos:text_size="50px"
ohos:layout_alignment="horizontal_center"/>
</DirectionalLayout>
可以看到熟悉的HelloWord,下面說說不同點(diǎn)
1趣苏、原本的android:變成了ohos:
2、DirectionalLayout定向性布局相當(dāng)于安卓的LinearLayout線性布局
3梯轻、組件命名更加直接TextView叫做Text
編寫簡單的點(diǎn)擊button切換圖片
xml文件
我們需要三個(gè)組件text食磕、button、image喳挑,text保持不變
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:height="80px"
ohos:width="match_content"
ohos:text="HelloHarmony"
ohos:text_size="50px"
ohos:top_margin="100px"
ohos:layout_alignment="horizontal_center"/>
<Button
ohos:id="$+id:test_button"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="ButtonTest"
ohos:text_size="100px"
ohos:layout_alignment="horizontal_center"
ohos:padding="10px"
ohos:text_color="#FFFFFF"
ohos:background_element="#6BBEFF"/>
<Image
ohos:id="$+id:test_img"
ohos:height="match_content"
ohos:width="match_content"
ohos:image_src="$media:img_test"/>
</DirectionalLayout>
依然使用定向性布局
組件命名:$+id:
大小單位有三種px彬伦、fp、vp
px:像素
vp:以屏幕相對像素為單位伊诵,也就是安卓中的dp
fp:相當(dāng)于sp
組件的寬高
match_content相當(dāng)于安卓wrop_parent
match_parent同安卓
圖片的引入方法
ohos:image_src="$media:img_test"
media是圖片的存放文件夾
java代碼
public class MainAbilitySlice extends AbilitySlice {
private Text text;
private Button button;
private Image image;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
}
private void initView(){
button = (Button) findComponentById(ResourceTable.Id_test_button);
image = (Image) findComponentById(ResourceTable.Id_test_img);
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
button.setText("TestSuccess");
image.setImageAndDecodeBounds(ResourceTable.Media_img_test2);
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
組件關(guān)聯(lián)與安卓基本一樣单绑,不同之處在于
定義組件類型時(shí)要選擇上面這個(gè)
使用findComponentById并且需要強(qiáng)轉(zhuǎn)
然后我們注意到
ResourceTable.Id_test_button
ResourceTable此方法引用layout、組件曹宴、圖片不用之處在點(diǎn)后面
我們可以注意到我們xml名字為ability_main但是他引用時(shí)為:ResourceTable.Layout_ability_main
引用組件時(shí)我們的命名是test_button他引用時(shí)為:ResourceTable.Id_test_button
圖片我們命名為img_test2并且放在media文件夾搂橙,他的寫法為ResourceTable.Media_img_test2
由此可得鴻蒙會(huì)自動(dòng)在你的文件或組件的命名前面加上相應(yīng)的屬性,然后只需要統(tǒng)一使用ResourceTable
編譯
由于我沒有鴻蒙系統(tǒng)的手機(jī)所以創(chuàng)建了虛擬機(jī)
流程為:點(diǎn)擊頂部Tools->HVD manager
點(diǎn)擊后需要使用你的開發(fā)者賬號(hào)登錄笛坦,然后創(chuàng)建虛擬機(jī)
點(diǎn)擊P40后面的三角開始運(yùn)行虛擬機(jī)
虛擬機(jī)打開后我們點(diǎn)擊編譯項(xiàng)目
(我使用的特別喜歡的WLOP大神的圖区转,侵權(quán)聯(lián)系刪)
然后我們點(diǎn)擊button會(huì)發(fā)現(xiàn)button文字和圖片的改變
總結(jié)
以上為我編寫第一個(gè)鴻蒙demo的過程,只是實(shí)現(xiàn)了最簡單的功能版扩,個(gè)人感覺安卓開發(fā)者上手并不難废离,因?yàn)椴粫?huì)JS所以不知道js項(xiàng)目是什么樣的
鴻蒙的很多布局方式組件屬性等等都還只是簡單地看了下官方文檔,等有時(shí)間了再和大家分享更復(fù)雜的內(nèi)容