13.鸿蒙HarmonyOS App(JAVA)文本框组件按钮点击提示

news/2024/7/21 12:30:41 标签: harmonyos, 华为

13.鸿蒙HarmonyOS App(JAVA)文本框按钮点击提示

点击按钮触发组件状态,点击改变颜色

文本框组件,文本居中,斜体,左右对齐,点击显示提示信息

Button button=(Button) findComponentById(ResourceTable.Id_btn_1);
button.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
        new ToastDialog(getContext())
                .setText("登录信息提示")
                .setAlignment(LayoutAlignment.CENTER)
                .show();


    }
});

字体设置

Text text_bold = (Text) findComponentById(ResourceTable.Id_text_bold);
text_bold.setFont(Font.DEFAULT_BOLD);
text_bold.setTextColor(Color.BLUE);

text,textfiled控件

编辑器动作:

 TextField textField = (TextField) findComponentById(ResourceTable.Id_text_filed_user_name);
 // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEARCH);

// textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_GO);
// textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEND);
 textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_UNSPECIFIED);

MainAbilitySlice
package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text_bold = (Text) findComponentById(ResourceTable.Id_text_bold);
        text_bold.setFont(Font.DEFAULT_BOLD);
        text_bold.setTextColor(Color.BLUE);
        TextField textField = (TextField) findComponentById(ResourceTable.Id_text_filed_user_name);
         textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEARCH);
       // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_GO);
       // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEND);
       // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_UNSPECIFIED);
        Button button=(Button) findComponentById(ResourceTable.Id_btn_1);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext())
                        .setText("登录信息提示")
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();


            }
        });

    }

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

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

 layout 

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:alignment="center"
    ohos:orientation="vertical">

        <Text
            ohos:id="$+id:text_user_name"
            ohos:height="match_content"
            ohos:width="350vp"
            ohos:background_element="$graphic:background_ability_main"

            ohos:text="用户名:"
            ohos:text_size="40vp"
            />
        <TextField
            ohos:id="$+id:text_filed_user_name"
            ohos:height="match_content"
            ohos:background_element="#FC0A84EF"
            ohos:width="350vp"
            ohos:hint="请输入用户名"

            ohos:margin="2vp"
            ohos:text_size="40vp"


            />

    <Text
        ohos:id="$+id:text_pass"
        ohos:height="match_content"
        ohos:width="350vp"
        ohos:background_element="$graphic:background_ability_main"

        ohos:text="密码:"
        ohos:text_size="40vp"
        />
    <TextField
        ohos:id="$+id:text_filed_ps"
        ohos:height="match_content"
        ohos:background_element="#FC0A84EF"
        ohos:width="350vp"
        ohos:hint="请输入密码"
        ohos:text_input_type="pattern_password"

        ohos:margin="2vp"
        ohos:text_size="40vp"

        />
    <Text
        ohos:id="$+id:text_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:btn_state_container"
        ohos:layout_alignment="horizontal_center"
        ohos:text="测试文本"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_bold"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="加粗文本"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_xieti"
        ohos:height="match_content"
        ohos:width="390vp"
        ohos:background_element="#FF84F136"
        ohos:layout_alignment="horizontal_center"
        ohos:text="斜体文本"
        ohos:italic="true"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_2"
        ohos:height="match_content"
        ohos:width="390vp"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="左对齐"
        ohos:text_alignment="left"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_3"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="居中文本"
        ohos:text_alignment="center"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_3a"
        ohos:height="match_content"
        ohos:background_element="#FC0A84EF"
        ohos:width="390vp"
        ohos:text_alignment="right"
        ohos:text="右对齐文本"
        ohos:margin="2vp"
        ohos:text_size="40vp"

        />
    <Text
        ohos:id="$+id:text_3b"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="多行文本1234567890多行文本多行文本多行文本多行文本多行文"
        ohos:multiple_lines="true"
        ohos:text_size="40vp"
        />
    <Button
        ohos:id="$+id:btn_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:btn_state_container"
        ohos:layout_alignment="horizontal_center"
        ohos:text="登录按钮"

        ohos:text_size="40vp"
        />


</DirectionalLayout>

graphic

btn_state_container.xml

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <item ohos:state ="component_state_pressed" ohos:element ="#FF4286EE"/><!--按下切换到该状态 -->
    <item ohos:state ="component_state_hovered" ohos:element ="#FF76EE42"/><!--触摸设备不会切换到该状态 -->
    <item ohos:state ="component_state_empty" ohos:element ="#FF65D9EE"/><!--默认状态 -->
    <item ohos:state ="component_state_selected" ohos:element ="#FFE7F14A"/><!--默认状态 -->
</state-container>


http://www.niftyadmin.cn/n/5288449.html

相关文章

cfa一级考生复习经验分享系列(十四)

首先说一下自己的背景&#xff0c;一个和金融没有半毛钱关系的数据分析师&#xff0c;之前考出了FRM。这次用一个半月突击12月的1级考试拿到了9A1B的成绩&#xff0c;纯属运气。以下纯属经&#xff08;chě&#xff09;验&#xff08;dn&#xff09;&#xff0c;请看看就好&…

实现阿里云oos云存储,简单几步

一、前言 虽然平常学习用的不多&#xff0c;但是用的时候再去找官方文档&#xff0c;也很繁琐&#xff0c;不如直接整理以下&#xff0c;方便粘贴复制&#xff0c;本文介绍两种图片上传方式①普通上传②服务端签名直传 1.普通上传 加载maven依赖 <dependency><grou…

账号与权限管理

一、Linux用户 1.1用户类型 1.普通用户&#xff1a;权限受到限制的用户 2.超级管理员&#xff1a;至高无上的权限 3.程序用户&#xff1a;是给程序使用的&#xff0c;不允许登录(为了安全性考虑) 能不能打开文件和用户有关&#xff0c;用户有自己的权限 运行程序不能使用超…

C++图论之强连通图

1. 连通性 什么是连通性&#xff1f; 连通&#xff0c;字面而言&#xff0c;类似于自来水管道中的水流&#xff0c;如果水能从某一个地点畅通流到另一个地点&#xff0c;说明两点之间是连通的。也说明水管具有连通性&#xff0c;图中即如此。 无向图和有向图的连通概念稍有差…

RabbitMQ 常见问题

1. 如何保证消息顺序消费 在RabbitMQ中&#xff0c;消息最终会保存在队列中&#xff0c;在同一个队列中&#xff0c;消息是顺序的&#xff0c;保持先进先出的原则&#xff0c;这个由Rabbitmq保证。而不同队列中的消息&#xff0c;RabbitMQ 是无法保证其顺序性。顺序消费主要是…

【Vue】computed详解

✨ 专栏介绍 在当今Web开发领域中&#xff0c;构建交互性强、可复用且易于维护的用户界面是至关重要的。而Vue.js作为一款现代化且流行的JavaScript框架&#xff0c;正是为了满足这些需求而诞生。它采用了MVVM架构模式&#xff0c;并通过数据驱动和组件化的方式&#xff0c;使…

Kubernetes (四) 资源清单及yaml文件详解

一. 资源清单 二. 编写yaml文件及内容详解 常用命令 …

深入理解 golang 中的反射机制

目录 什么是反射 反射的使用方法 反射的应用场景 反射的性能考量 反射的最佳实践 小结 反射机制是计算机科学中的一个重要概念&#xff0c;程序通过反射可以在运行时访问、检测和修改自身的状态和行为。Golang 作为静态类型的编译型语言&#xff0c;虽然在设计上倾向于简…