我正在开发 Titanium 移动应用程序,但在使用 backgroundRadient 时遇到按钮聚焦问题。
我使用 touchstart 事件取得了成功(changeBackgroundGradient 非常简单,可以避免在此处显示):
authButton.addEventListener('touchstart', function(e) {
changeBackgroundGradient(authButton);
});
但是 touchend 事件不是我想要的。仅当用户结束元素上的触摸(表现为单击事件)时才会触发。 Touchmove 也不是我想要的,因为它会在用户移动时立即触发。
authButton.addEventListener('touchmove', function(e) {
revertBackgroundGradient(authButton);
});
我想要的是:只要用户触摸按钮,按钮就处于焦点状态。类似于“ontap”和“onrelease”。 我知道有backgroundFocusedColor和backgroundFocusedImage,但没有backgroundFocusedGradient。
使用backgroundGradient属性时如何处理按钮焦点? 我想要默认行为,但一旦我使用 backgroundGradient 属性,它似乎就会停止。
谢谢。
--编辑:
这是完整的代码:
// Authenticate Button
var authButton = Ti.UI.createButton({
width : '50%',
height : '60',
top : '15',
bottom : '15',
title : L('LoginView_authButton'),
font: {
fontSize: 20,
fontFamily: 'TrebuchetMS-Bold'
},
color : '#FFFFFF',
textAlign : 'center',
borderColor : '#3D86A9',
borderWidth : '2',
borderRadius : '5',
backgroundGradient : {
type : 'linear',
startPoint : {
x : '0%',
y : '0%'
},
endPoint : {
x : '0%',
y : '100%'
},
colors : [{
color : '#a2d6eb',
offset : 0.0
}, {
color : '#67afcf',
offset : 0.5
}, {
color : '#3591bc',
offset : 0.5
}, {
color : '#1e83b1',
offset : 1.0
},
]
}
});
authButton.addEventListener('touchstart', function(e) {
changeBackgroundGradient(authButton);
});
authButton.addEventListener('touchend', function(e) {
revertBackgroundGradient(authButton);
});
function changeBackgroundGradient(AuthButton)
{
AuthButton.backgroundGradient = {
type : 'linear',
startPoint : {
x : '0%',
y : '0%'
},
endPoint : {
x : '0%',
y : '100%'
},
colors : [{
color : '#f5bd8b',
offset : 0.0
}, {
color : '#e59a57',
offset : 0.5
}, {
color : '#da7a23',
offset : 0.5
}, {
color : '#c35211',
offset : 1.0
},
]
};
}
function revertBackgroundGradient(AuthButton)
{
AuthButton.backgroundGradient = {
type : 'linear',
startPoint : {
x : '0%',
y : '0%'
},
endPoint : {
x : '0%',
y : '100%'
},
colors : [{
color : '#9FD3E9',
offset : 0.0
}, {
color : '#63AAC9',
offset : 0.5
}, {
color : '#348BB8',
offset : 0.5
}, {
color : '#2081B2',
offset : 1.0
},
]
};
}
最佳答案
哦,好吧,我现在明白了,它按预期工作,这不是一个错误,因为您将事件监听器添加到按钮,所以它仅适用于按钮。
您可能想向 authButton 添加一个名为 isFocused 的属性或类似的属性。在 touchstart 事件上添加:
authButton.isFocused:true;
然后在包含具有恢复功能的 authButton 的 View /窗口上添加 touchend 事件。
你明白为什么吗?
关于android - 钛按钮.background渐变焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11761795/