简体   繁体   English

设置CSS样式Uibinder Gwt

[英]setting CSS style Uibinder Gwt

I want to change the color of ag:label using java code on a onBlur event. 我想在onBlur事件上使用Java代码更改ag:label的颜色。 I am using eclipse, UIBinder. 我正在使用Eclipse,UIBinder。

This is what I have in mind although it doesn't work. 这是我要记住的,尽管它不起作用。

In my StandardDocumentDownload.ui.xml file 在我的StandardDocumentDownload.ui.xml文件中

<ui:style>     
   .testStyle {
   }
   .styleRequiredData
   {
        color:red;

    }
 </ui:style>

this is the event in my standardDocumentDownload.java file 这是我的standardDocumentDownload.java文件中的事件

@UiHandler("comboTitle")
void onComboTitleBlur(BlurEvent event) {
    int title = comboTitle.getSelectedIndex();

    if(title == 0)
    {
        labTitleReq.setText("Please enter a value");
        labTitle.addStyleName("styleRequiredData");
    }
    else
    {
        labTitleReq.setText("");
    }

}

How could I add the color red to the existing style of the label upon the firing of the event. 事件触发后,如何将红色添加到标签的现有样式中。

Kind regards 亲切的问候

See here under Programmatic access to inline Styles 在此处查看对内联样式的编程访问

for you, it shoulbe be something like : 对您来说,应该是这样的:

<ui:style type="com.yourapp.YourClass.MyStyle">     
    .testStyle {
    }
    .styleRequiredData
    {
        color:red;
    }
</ui:style>

public class YourClass extends Widget {
    interface MyStyle extends CssResource {
        String testStyle();
        String styleRequiredData();
    }

    @UiField MyStyle style;

    /* ... */

    @UiHandler("comboTitle")
    void onComboTitleBlur(BlurEvent event) {
        int title = comboTitle.getSelectedIndex();
        if(title == 0){
            labTitleReq.setText("Please enter a value");
            labTitle.getElement().addClassName(style.styleRequiredData);
        } else {
            labTitleReq.setText("");
        }
    }
}

Took me a while to find it but the Documentation; 花了我一段时间才找到它,但是文档; " Declarative Layout with UiBinder: Programmatic access to inline Styles " tells you how. 使用UiBinder进行声明式布局:以编程方式访问内联样式 ”将告诉您如何。 Here the code snippets 这里的代码片段

UiBinder: UiBinder:

  <ui:style type='com.my.app.MyFoo.MyStyle'>
    .redBox { background-color:pink; border: 1px solid red; }
    .enabled { color:black; }
    .disabled { color:gray; }
  </ui:style>

  <div class='{style.redBox} {style.enabled}'>I'm a red box widget.</div>

</ui:UiBinder>

Code behind: 后面的代码:

public class MyFoo extends Widget {
  interface MyStyle extends CssResource {
    String enabled();
    String disabled();
  }

  @UiField MyStyle style;

  /* ... */

  void setEnabled(boolean enabled) {
    getElement().addClassName(enabled ? style.enabled() : style.disabled());
    getElement().removeClassName(enabled ? style.disabled() : style.enabled());
  }
}

Description: 描述:

The element has a new attribute, type='com.my.app.MyFoo.MyStyle'. 元素具有一个新属性,类型为'com.my.app.MyFoo.MyStyle'。 That means that it needs to implement that interface (defined in the Java source for the MyFoo widget above) and provide the two CSS classes it calls for, enabled and disabled. 这意味着它需要实现该接口(在上面的MyFoo小部件的Java源代码中定义),并提供它调用的两个CSS类(启用和禁用)。

Now look at the @UiField MyStyle style; 现在看看@UiField MyStyle样式; field in MyFoo.java. MyFoo.java中的字段。 That gives the code access to the CssResource generated for the block. 这使代码可以访问为该块生成的CssResource。 The setEnabled method uses that field to apply the enabled and disabled styles as the widget is turned on and off. 当窗口小部件打开和关闭时,setEnabled方法使用该字段来应用启用和禁用的样式。

You're free to define as many other classes as you like in a style block with a specified type, but your code will have access only to those required by the interface. 您可以在具有指定类型的样式块中自由定义其他任意多个类,但是您的代码将只能访问该接口所需的那些类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM