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

2023-12-26 22:52:00

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>

文章来源:https://blog.csdn.net/txwtech/article/details/135232250
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。