栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

页面跳转

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

页面跳转

文章目录[隐藏]
  • 编写第一个页面(使用xml)
  • 编写第二个页面(使用Java)
  • 添加单击事件
编写页面

编写页面有两个方式,一是通过xml文件,二是通过Java代码。

编写第一个页面(使用xml)

直接改写默认的MainAbility,如下




    

    

编写第二个页面(使用Java)

先新建一个页面,如下

开发工具会自动生成一些文件,如下

在此我们不用xml编写,因此可以将3删掉,在1中编写代码:

public class SecondAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setUIContent(ResourceTable.Layout_ability_second);
        // 声明布局
        DirectionalLayout myLayout = new DirectionalLayout(this);
        // 设置布局背景为白色
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(background);
        // 创建一个文本
        Text text = new Text(this);
        text.setText("第二个界面");
        text.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
        text.setTextSize(100);
        text.setTextColor(Color.BLACK);
        // 设置文本的布局
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);
        textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        text.setLayoutConfig(textConfig);
        //把文本对象添加到布局中
        myLayout.addComponent(text);
        //把布局添加到界面中
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

添加单击事件
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Button button = (Button) findComponentById(ResourceTable.Id_button1);
        // 点击按钮跳转至第二个页面
        button.setClickedListener(listener -> present(new SecondAbilitySlice(), new Intent()));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

此时运行就可以看到效果了


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/352034.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号