一個大二學生黨第一次發文章,最近開始接觸鴻蒙軟體開發,相對于安卓開發,個人覺得鴻蒙開發有更多的選擇性,接下來直接上自己做的一個小demo

主界面


通過getText()方法來獲取文本框里面的資訊進行賬號密碼判斷
登錄失敗回傳登錄界面
登錄成功跳轉界面

登錄成功界面的文字是用了跑馬燈的顯示方式
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:text_hello"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:head"
ohos:text_size="120"
/>
<TabList
ohos:id="$+id:tab_list"
ohos:height="40vp"
ohos:width="match_parent"
ohos:tab_length="90vp"
ohos:text_size="20fp"
ohos:text_alignment="center"
ohos:orientation="horizontal"
ohos:normal_text_color="#000000"
ohos:selected_text_color="#000000"
ohos:selected_tab_indicator_color="#000000"
ohos:selected_tab_indicator_height="10vp"
/>
<DirectionalLayout
ohos:id="$+id:tab_container"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:below="$id:tab_list"
/>
</DirectionalLayout>
login.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:text_body"
ohos:height="100vp"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:login"
ohos:text_size="120"
ohos:top_padding="80"
/>
<TextField
ohos:hint="請輸入賬號..."
ohos:hint_color="#000000"
ohos:left_margin="40vp"
ohos:right_margin="40vp"
ohos:id="$+id:username"
ohos:below="$id:text_body"
ohos:height="40vp"
ohos:width="match_parent"
ohos:background_element="#FF75758B"
ohos:auto_font_size="true"
ohos:multiple_lines="false"
ohos:text_input_type="pattern_text"
/>
<TextField
ohos:hint="請輸入密碼..."
ohos:hint_color="#000000"
ohos:top_margin="10vp"
ohos:left_margin="40vp"
ohos:right_margin="40vp"
ohos:id="$+id:password"
ohos:below="$id:username"
ohos:height="40vp"
ohos:width="match_parent"
ohos:background_element="#FF75758B"
ohos:auto_font_size="true"
ohos:multiple_lines="false"
ohos:text_input_type="pattern_password"
/>
<Button
ohos:top_margin="20vp"
ohos:left_margin="120vp"
ohos:right_margin="120vp"
ohos:height="40vp"
ohos:width="match_parent"
ohos:id="$+id:loginbutton"
ohos:hint="登錄"
ohos:hint_color="#000000"
ohos:background_element="#FF75758B"
ohos:auto_font_size="true"
/>
</DirectionalLayout>
hello.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:text_body"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:Hello"
ohos:text_size="120"
ohos:top_padding="80"
/>
</DirectionalLayout>
loginfinish.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:top_text_body"
ohos:height="100vp"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:loginfinish"
ohos:text_size="120"
ohos:top_padding="80"
/>
<Image
ohos:id="$+id:wx"
ohos:height="100vp"
ohos:width="match_content"
ohos:below="$id:top_text_body"
ohos:image_src="$media:weixin"
ohos:scale_y="4"
ohos:scale_x="4"
ohos:layout_alignment="horizontal_center"
/>
<Text
ohos:id="$+id:bottom_text_body"
ohos:height="100vp"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:loginwx"
ohos:below="$id:wx"
ohos:text_size="120"
ohos:top_padding="80"
/>
</DirectionalLayout>
注冊界面和關于我們的界面只是個模子我還沒做所以就跳過了
MainAbilitySlice.java
package com.etc.tablelist.slice;
import com.etc.tablelist.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import java.text.BreakIterator;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
/*頂部切換控制元件 tablist*/
TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
TabList.Tab tab1 = tabList.new Tab(getContext());
tab1.setText("首頁");
tabList.addTab(tab1);
TabList.Tab tab2 = tabList.new Tab(getContext());
tab2.setText("登錄");
tabList.addTab(tab2);
TabList.Tab tab3 = tabList.new Tab(getContext());
tab3.setText("注冊");
tabList.addTab(tab3);
TabList.Tab tab4 = tabList.new Tab(getContext());
tab4.setText("關于我們");
tabList.addTab(tab4);
AbilitySlice slice=this;//指向當前物件
//添加tablist的選擇效果
tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
ComponentContainer componentContainer = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);
@Override
public void onSelected(TabList.Tab tab) {
if(tab.getText().equals("首頁")){//當點首頁時顯示首頁
componentContainer.removeAllComponents();//清除當前界面
Component hello = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_hello,null,false);
componentContainer.addComponent(hello);//顯示首頁
}else if(tab.getText().equals("登錄")){//當點登錄時顯示登錄界面
componentContainer.removeAllComponents();//清除當前界面
Component login = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_login,null,false);
componentContainer.addComponent(login);//顯示登錄界面
Button button = (Button) findComponentById(ResourceTable.Id_loginbutton);
button.setClickedListener(new Component.ClickedListener() {//添加按鈕的選擇效果
@Override
public void onClick(Component component) {
/*獲取帳號和密碼*/
Text username = (Text) findComponentById(ResourceTable.Id_username);
Text password = (Text) findComponentById(ResourceTable.Id_password);
if(username.getText().trim().equals("Leach")&&password.getText().trim().equals("001216")){ //判斷帳號和密碼是否匹配
componentContainer.removeAllComponents();
MainAbilitySlice.super.setUIContent(ResourceTable.Layout_loginfinish);
Text text= (Text) findComponentById(ResourceTable.Id_bottom_text_body);
//設定跑馬燈效果
text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
//啟動跑馬燈
text.startAutoScrolling();
//設定始終
text.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
}else{
componentContainer.removeAllComponents();
componentContainer.addComponent(login);
}
}
});
}else if(tab.getText().equals("注冊")){
componentContainer.removeAllComponents();
Component register = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_register,null,false);
componentContainer.addComponent(register);
}else if(tab.getText().equals("關于我們")){
componentContainer.removeAllComponents();
Component aboutus = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_aboutus,null,false);
componentContainer.addComponent(aboutus);
}
}
@Override
public void onUnselected(TabList.Tab tab) {
}
@Override
public void onReselected(TabList.Tab tab) {
}
});
tabList.selectTab(tab1);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
string.json
{
"string": [
{
"name": "app_name",
"value": "tablelist"
},
{
"name": "head",
"value": "廈門同城交友"
},
{
"name": "mainability_description",
"value": "Java_Phone_Empty Feature Ability"
},
{
"name": "Hello",
"value": "歡迎使用"
},
{
"name": "login",
"value": "登錄界面"
},
{
"name": "register",
"value": "注冊界面"
},
{
"name": "aboutus",
"value": "關于我們"
},
{
"name": "loginfinish",
"value": "登錄成功"
},
{
"name": "loginwx",
"value": "最快捷的交友方式就是微信啦!!!"
}
]
}
以上就是demo的全內容
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289574.html
標籤:java
