显然这应该非常直观,但我无法让它工作,当点击一个按钮我应该改变它的风格,如果再次点击,将风格恢复到它的样式。我的猜测是:
@Override
public void buttonClick(ClickEvent event) {
Button b = event.getButton();
if (b == my_special_button){
if(!b.getStyleName().contains("x")){
b.addStyleName("x");
} else {
b.removeStyleName("x");
}
}
}
使用chrome检查器时,我看到添加了活动类,但按钮消失了。如果我单击按钮所在的位置,我会看到“活动”类被删除(如果再次单击则添加)。只有当按钮在区域外单击时,该按钮才会重新出现。
你能提供一些见解吗?
编辑:我在这里修正了示例代码(在我的课程中已经如建议的那样,我不知道它有所作为,所以谢谢!)
当页面加载时,按钮具有以下类:
v-button v-button-thumbs-up-button thumbs-up-button
当我点击时,使用chrome检查器我可以看到已激活类激活:
v-button v-button-thumbs-up-button thumbs-up-button v-button-active active
如果我再次点击,它就会被删除。显然,onclick代码正确执行。
有两个问题:
进一步参考这是我正在使用的CSS:
.thumbs-up-button{
background-image: url("../covercliptheme/img/thumbs_up_1x.png");
background-position: left top;
}
.thumbs-up-button .v-button-active .active{
background-image: url("../covercliptheme/img/thumbs_up_1x_green.png");
background-position: left top;
}
我找到了样式的解决方法:主动和:专注。它正在发挥作用,但它没有真正的理由。它应该像我原先想象的那样工作,通过添加一个类,按钮用该样式呈现,删除类,样式回到原始样式。
我认为问题在于我使用组件而不是在onClick操作本身构建页面的方式。我非常好奇地发现出了什么问题:)按钮是组件的一部分,它是其他组件的一部分,特别是带有按钮的组件由以下类表示:
public class CVRow extends CustomComponent implements Button.ClickListener{
@AutoGenerated
private AbsoluteLayout mainLayout;
@AutoGenerated
private HorizontalLayout horizontalLayout_1;
@AutoGenerated
private AbsoluteLayout absoluteLayout_2;
@AutoGenerated
private Button nativeButton_2;
@AutoGenerated
private Button nativeButton_1;
/**
* The constructor
*/
public CVRow() {
buildMainLayout();
setCompositionRoot(mainLayout);
nativeButton_1.addListener(this);
// TODO add user code here
}
@AutoGenerated
private AbsoluteLayout buildMainLayout() {
/* some layout code.. */
// add horizontalLayout_1
horizontalLayout_1 = buildHorizontalLayout_1();
mainLayout.addComponent(horizontalLayout_1, "top:0.0px;left:0.0px;");
return mainLayout;
}
@AutoGenerated
private HorizontalLayout buildHorizontalLayout_1() {
// some layout code... //
// add absoluteLayout_2 <-- buttons will be here
absoluteLayout_2 = buildAbsoluteLayout_2();
horizontalLayout_1.addComponent(absoluteLayout_2);
return horizontalLayout_1;
}
@AutoGenerated
private AbsoluteLayout buildAbsoluteLayout_2() {
// common part: create layout
absoluteLayout_2 = new AbsoluteLayout();
absoluteLayout_2.setImmediate(false);
absoluteLayout_2.setWidth("60px");
absoluteLayout_2.setHeight("60px");
absoluteLayout_2.setMargin(false);
//BUTTONS are here:
// nativeButton_1
nativeButton_1 = new Button();
nativeButton_1.setImmediate(true);
nativeButton_1.setWidth("20px");
nativeButton_1.setHeight("20px");
nativeButton_1.setStyleName("thumbs-up-button");
absoluteLayout_2.addComponent(nativeButton_1, "top:41.0px;left:0.0px;");
// nativeButton_2
nativeButton_2 = new Button();
nativeButton_2.setStyleName("thumbs-down-button");
nativeButton_2.setImmediate(true);
nativeButton_2.setWidth("20px");
nativeButton_2.setHeight("20px");
absoluteLayout_2.addComponent(nativeButton_2, "top:41.0px;left:40.0px;");
return absoluteLayout_2;
}
@Override
public void buttonClick(ClickEvent event) {
Button b = event.getButton();
if (b == nativeButton_1){
if(nativeButton_1.getStyleName().contains("active"))
nativeButton_1.removeStyleName("active");
else
nativeButton_1.addStyleName("active");
}
//etc...
}
}
答案 0 :(得分:0)
您的代码的问题是,您检查并更改按钮“b”中的样式名称而不是按钮“my_special_button”。它必须如下所示:
@Override
public void buttonClick(ClickEvent event) {
Button b = event.getButton();
if (b == my_special_button){
if(!my_special_button.getStyleName().contains("x")){
my_special_button.addStyleName("x");
} else {
my_special_button.removeStyleName("x");
}
}
}
答案 1 :(得分:-1)
使用纯javascript执行此操作的简单示例:DEMO
HTML:
<button id="toggleButton">
Toggle Class
</button>
CSS:
button{
font-size:10px;
}
.active{
font-size:15px;
}
JS:
var btn = document.getElementById('toggleButton');
btn.addEventListener('click', function(event){
if(this.classList.contains('active')){
this.classList.remove('active');
}
else{
this.classList.add('active');
}
});