鸿蒙应用开发练习:设置文本内容大小,位置,颜色,监听器等
头像 Tiechui.Wang 2021-03-01 10:00:00    发布
3481 浏览 11 点赞 19 收藏

鸿蒙应用开发练习:设置文本内容大小,位置,颜色,监听器等-鸿蒙开发者社区

鸿蒙应用开发练习:设置文本内容大小,位置,颜色,监听器等-鸿蒙开发者社区




鸿蒙应用开发练习:设置文本内容大小,位置,颜色,监听器等-鸿蒙开发者社区

MainAbility.java

package com.example.abilityui;

import com.example.abilityui.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class MainAbility extends Ability {
   @Override
   public void onStart(Intent intent) {
       super.onStart(intent);

       initUIbyXML();//这里结合了xml和java  xml进行界面布局  java进行逻辑编写
//        initUIbyJava();//这里调用的是java封装好的布局
   }

   private void initUIbyXML(){
       setUIContent(ResourceTable.Layout_Ability_ui);
       Text text = (Text)findComponentById(ResourceTable.Id_test_text);
       Button button = (Button) findComponentById(ResourceTable.Id_test_button);

       ShapeElement element = new ShapeElement();

       element.setCornerRadius(30);
       element.setRgbColor(new RgbColor(43,156,15));

       //给按钮设置监听器
       button.setClickedListener(new Component.ClickedListener() {
           //定义一个成员变量
           private int a =0;
           @Override
           public void onClick(Component component) {
               //每当按钮被点击了 a++
               a++;
               //改变文本框上面的内容
               text.setText("Clicked:"+a);
           }
       });
   }

   private void initUIbyJava(){
       //定义一个容器对象,使用绝对定位来管理组件容器
       ComponentContainer container = new PositionLayout(this);

       //设置容器宽高,宽高和设备屏幕一样宽,一样高
       container.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
       container.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);

       //创建一个文本框对象
       Text text = new Text(this);
       //显示文本框要展示的内容和信息
       text.setText("Hello word!");
       //设置位置和大小
       text.setTop(100);//y
       text.setLeft(140);//x
       text.setWidth(200);//w
       text.setHeight(40);//h
       text.setTextColor(Color.RED);//设置颜色
       text.setTextSize(36);//字体大小
       text.setTextAlignment(TextAlignment.CENTER);//设置文本对齐方式

       //使用ShapeElement设置按钮的外观
       ShapeElement element = new ShapeElement();
       //这里设置颜色为绿色
       element.setRgbColor(new RgbColor(231,135,255));
       element.setCornerRadius(30);
       //创建一个按钮
       Button button = new Button(this);
       //设置按钮的位置
       button.setText("点我!");
       button.setTop(150);
       button.setLeft(130);
       button.setWidth(200);
       button.setHeight(40);
       //调用上面设置的颜色
       button.setBackground(element);
       //给按钮设置监听器
       button.setClickedListener(new Component.ClickedListener() {
           //定义一个成员变量
           private int a =0;
           @Override
           public void onClick(Component component) {
               //每当按钮被点击了 a++
               a++;
               //改变文本框上面的内容
               text.setText("Clicked:"+a);
           }
       });
       //将文本对象放到容器中去
       container.addComponent(text);
       container.addComponent(button);

       //当前的容器委托来管理组件
       setUIContent(container);
   }
}

Ability_ui.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">

   <Component ohos:width="match_parent"
              ohos:height="70"/>

   <Text ohos:id="$+id:test_text"
         ohos:text="Hello world!"
         ohos:width="match_content"
         ohos:height="match_content"
         ohos:layout_alignment="center"
         ohos:top_margin="30"
         ohos:text_size="40"/>

   <Button ohos:id="$+id:test_button"
           ohos:text="点我!"
           ohos:width="200"
           ohos:height="50"
           ohos:layout_alignment="center"
           ohos:top_margin="30"
           ohos:background_element="#FF00FF00"

           ohos:text_size="30"/>

</DirectionalLayout>

©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255