当前位置:   article > 正文

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

鸿蒙button组件java

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
  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.*;
  6. import ohos.agp.text.Font;
  7. import ohos.agp.utils.Color;
  8. import ohos.agp.utils.LayoutAlignment;
  9. import ohos.agp.window.dialog.ToastDialog;
  10. public class MainAbilitySlice extends AbilitySlice {
  11. @Override
  12. public void onStart(Intent intent) {
  13. super.onStart(intent);
  14. super.setUIContent(ResourceTable.Layout_ability_main);
  15. Text text_bold = (Text) findComponentById(ResourceTable.Id_text_bold);
  16. text_bold.setFont(Font.DEFAULT_BOLD);
  17. text_bold.setTextColor(Color.BLUE);
  18. TextField textField = (TextField) findComponentById(ResourceTable.Id_text_filed_user_name);
  19. textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEARCH);
  20. // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_GO);
  21. // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEND);
  22. // textField.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_UNSPECIFIED);
  23. Button button=(Button) findComponentById(ResourceTable.Id_btn_1);
  24. button.setClickedListener(new Component.ClickedListener() {
  25. @Override
  26. public void onClick(Component component) {
  27. new ToastDialog(getContext())
  28. .setText("登录信息提示")
  29. .setAlignment(LayoutAlignment.CENTER)
  30. .show();
  31. }
  32. });
  33. }
  34. @Override
  35. public void onActive() {
  36. super.onActive();
  37. }
  38. @Override
  39. public void onForeground(Intent intent) {
  40. super.onForeground(intent);
  41. }
  42. }

 layout 

ability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:text_user_name"
  10. ohos:height="match_content"
  11. ohos:width="350vp"
  12. ohos:background_element="$graphic:background_ability_main"
  13. ohos:text="用户名:"
  14. ohos:text_size="40vp"
  15. />
  16. <TextField
  17. ohos:id="$+id:text_filed_user_name"
  18. ohos:height="match_content"
  19. ohos:background_element="#FC0A84EF"
  20. ohos:width="350vp"
  21. ohos:hint="请输入用户名"
  22. ohos:margin="2vp"
  23. ohos:text_size="40vp"
  24. />
  25. <Text
  26. ohos:id="$+id:text_pass"
  27. ohos:height="match_content"
  28. ohos:width="350vp"
  29. ohos:background_element="$graphic:background_ability_main"
  30. ohos:text="密码:"
  31. ohos:text_size="40vp"
  32. />
  33. <TextField
  34. ohos:id="$+id:text_filed_ps"
  35. ohos:height="match_content"
  36. ohos:background_element="#FC0A84EF"
  37. ohos:width="350vp"
  38. ohos:hint="请输入密码"
  39. ohos:text_input_type="pattern_password"
  40. ohos:margin="2vp"
  41. ohos:text_size="40vp"
  42. />
  43. <Text
  44. ohos:id="$+id:text_1"
  45. ohos:height="match_content"
  46. ohos:width="match_content"
  47. ohos:background_element="$graphic:btn_state_container"
  48. ohos:layout_alignment="horizontal_center"
  49. ohos:text="测试文本"
  50. ohos:text_size="40vp"
  51. />
  52. <Text
  53. ohos:id="$+id:text_bold"
  54. ohos:height="match_content"
  55. ohos:width="match_content"
  56. ohos:background_element="$graphic:background_ability_main"
  57. ohos:layout_alignment="horizontal_center"
  58. ohos:text="加粗文本"
  59. ohos:text_size="40vp"
  60. />
  61. <Text
  62. ohos:id="$+id:text_xieti"
  63. ohos:height="match_content"
  64. ohos:width="390vp"
  65. ohos:background_element="#FF84F136"
  66. ohos:layout_alignment="horizontal_center"
  67. ohos:text="斜体文本"
  68. ohos:italic="true"
  69. ohos:text_size="40vp"
  70. />
  71. <Text
  72. ohos:id="$+id:text_2"
  73. ohos:height="match_content"
  74. ohos:width="390vp"
  75. ohos:background_element="$graphic:background_ability_main"
  76. ohos:layout_alignment="horizontal_center"
  77. ohos:text="左对齐"
  78. ohos:text_alignment="left"
  79. ohos:text_size="40vp"
  80. />
  81. <Text
  82. ohos:id="$+id:text_3"
  83. ohos:height="match_content"
  84. ohos:width="match_content"
  85. ohos:background_element="$graphic:background_ability_main"
  86. ohos:layout_alignment="horizontal_center"
  87. ohos:text="居中文本"
  88. ohos:text_alignment="center"
  89. ohos:text_size="40vp"
  90. />
  91. <Text
  92. ohos:id="$+id:text_3a"
  93. ohos:height="match_content"
  94. ohos:background_element="#FC0A84EF"
  95. ohos:width="390vp"
  96. ohos:text_alignment="right"
  97. ohos:text="右对齐文本"
  98. ohos:margin="2vp"
  99. ohos:text_size="40vp"
  100. />
  101. <Text
  102. ohos:id="$+id:text_3b"
  103. ohos:height="match_content"
  104. ohos:width="match_content"
  105. ohos:background_element="$graphic:background_ability_main"
  106. ohos:layout_alignment="horizontal_center"
  107. ohos:text="多行文本1234567890多行文本多行文本多行文本多行文本多行文"
  108. ohos:multiple_lines="true"
  109. ohos:text_size="40vp"
  110. />
  111. <Button
  112. ohos:id="$+id:btn_1"
  113. ohos:height="match_content"
  114. ohos:width="match_content"
  115. ohos:background_element="$graphic:btn_state_container"
  116. ohos:layout_alignment="horizontal_center"
  117. ohos:text="登录按钮"
  118. ohos:text_size="40vp"
  119. />
  120. </DirectionalLayout>

graphic

btn_state_container.xml

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/220275
推荐阅读
相关标签
  

闽ICP备14008679号