当前位置:   article > 正文

鸿蒙app获取文本控件按钮控件_修改控件名称_按钮触发事件_提示信息显示_鸿蒙ets 控件id取得

鸿蒙ets 控件id取得

鸿蒙app获取文本控件按钮控件_修改控件名称_按钮触发事件_

点击启动:提示信息显示

  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.Button;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.components.DirectionalLayout;
  8. import ohos.agp.components.Text;
  9. import ohos.agp.utils.LayoutAlignment;
  10. import ohos.agp.window.dialog.ToastDialog;
  11. import ohos.app.Context;
  12. import ohos.hiviewdfx.HiLog;
  13. import ohos.hiviewdfx.HiLogLabel;
  14. public class MainAbilitySlice extends AbilitySlice {
  15. static final HiLogLabel loglabel2 = new HiLogLabel(HiLog.LOG_APP,0x001101,"MainAbility");
  16. @Override
  17. public void onStart(Intent intent) {
  18. super.onStart(intent);
  19. super.setUIContent(ResourceTable.Layout_ability_main);
  20. HiLog.info(loglabel2,"onStart-----------------------------------");
  21. int ppi = getContext().getResourceManager().getDeviceCapability().screenDensity;
  22. HiLog.info(loglabel2,"屏幕密度:"+Integer.toString(ppi));
  23. // Text text2 = new Text();
  24. DirectionalLayout.LayoutConfig configForText = new DirectionalLayout.LayoutConfig(
  25. toPixels(100,getContext()),
  26. toPixels(16,getContext())
  27. );
  28. //text2.setLayoutConfig(configForText);//如果报错,则导入import ohos.agp.components.Text;
  29. // text2.set
  30. Text text1 = (Text) findComponentById(ResourceTable.Id_text_1);
  31. Text text2 = (Text) findComponentById(ResourceTable.Id_text_2);
  32. Text text3 = (Text) findComponentById(ResourceTable.Id_text_3);
  33. Text text3a = (Text) findComponentById(ResourceTable.Id_text_3a);
  34. text1.setText("美好时光1");
  35. text2.setText("万事如意");
  36. text3.setText("好运常在");
  37. text3a.setText("鸿蒙app demo");
  38. Button button = (Button) findComponentById(ResourceTable.Id_btn1);
  39. button.setText("启动");
  40. button.setClickedListener(new Component.ClickedListener() {
  41. @Override
  42. public void onClick(Component component) {
  43. new ToastDialog(getContext())
  44. .setText("This is a ToastDialog")
  45. .setAlignment(LayoutAlignment.CENTER)
  46. .show();
  47. }
  48. });
  49. }
  50. /**
  51. *虚拟像素转物理像素
  52. */
  53. private static int toPixels(int value, Context context)
  54. {
  55. return value * context.getResourceManager().getDeviceCapability().screenDensity/160;
  56. }
  57. @Override
  58. public void onActive() {
  59. super.onActive();
  60. }
  61. @Override
  62. public void onForeground(Intent intent) {
  63. super.onForeground(intent);
  64. }
  65. }

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:scrollbar_background_color="#FF4385E7"
  7. ohos:orientation="vertical">
  8. <Button
  9. ohos:id="$+id:btn1"
  10. ohos:text="$string:btn_name"
  11. ohos:height="match_content"
  12. ohos:width="match_parent"
  13. ohos:text_size ="40vp"
  14. ohos:background_element="$graphic:capsule_button_element"
  15. ohos:layout_alignment="horizontal_center"
  16. />
  17. <Text
  18. ohos:id="$+id:text_1"
  19. ohos:height="match_content"
  20. ohos:width="match_content"
  21. ohos:background_element="$graphic:background_ability_main"
  22. ohos:layout_alignment="horizontal_center"
  23. ohos:text="$string:mainability_HelloWorld"
  24. ohos:text_size="40vp"
  25. />
  26. <Text
  27. ohos:id="$+id:text_2"
  28. ohos:height="match_content"
  29. ohos:width="match_content"
  30. ohos:background_element="$graphic:background_ability_main"
  31. ohos:layout_alignment="horizontal_center"
  32. ohos:text="$string:mainability_HelloWorld2"
  33. ohos:text_size="40vp"
  34. />
  35. <Text
  36. ohos:id="$+id:text_3"
  37. ohos:height="match_content"
  38. ohos:width="match_content"
  39. ohos:background_element="$graphic:background_ability_main"
  40. ohos:layout_alignment="horizontal_center"
  41. ohos:text="$string:mainability_HelloWorld3"
  42. ohos:text_size="40vp"
  43. />
  44. <Text
  45. ohos:id="$+id:text_3a"
  46. ohos:height="match_content"
  47. ohos:width="match_content"
  48. ohos:background_element="$graphic:background_ability_main"
  49. ohos:text="$string:mainability_HelloWorld3a"
  50. ohos:text_size="40vp"
  51. ohos:layout_alignment="horizontal_center"
  52. />
  53. </DirectionalLayout>

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

闽ICP备14008679号