我正在使用 Titanium 框架构建 iPhone 应用程序。我的应用程序中需要一个 5 星级评级控件,就像在应用程序商店中找到的那样。作为临时解决方案,我使用 slider 来添加评级。我在网上找到的大多数示例都在 Objective C 中。有人可以指导我使用钛来实现这一目标吗?
问候
最佳答案
您只需创建一个 View
并用您想要的按钮数量填充它,然后为它们分配一个点击事件。
var rateView = Ti.UI.createView(),
max = 5, min = 1; // used in the flexMin and flexMax
function rate(v) {
// rate code storage goes here
// your choice on if you want to have separate images of each rating or run a loop
// based on the value passed in to change a set number of stars to their alternate
// image
}
var flexMin = Ti.UI.createButton({
systemButton: Ti.UI.iPhone.SystemButton.FLEXIBLE_SPACE,
top: 0, left: 0,
height: rateView.height
});
flexMin.addEventListener('click', function(e) {
rate(min);
});
var flexMax = Ti.UI.createButton({
systemButton: Ti.UI.iPhone.SystemButton.FLEXIBLE_SPACE
top: 0, right: 0,
height: rateView.height
});
flexMax.addEventListener('click', function(e) {
rate(max);
});
rateView.add(flexMin);
rateView.add(flexMax);
var stars = [];
for(var i=1;i<max;i++) {
stars[i] = Ti.UI.createButton({
// styling including the darker or light colored stars you choose,
'rating': i
});
stars[i].addEventListener('click', function(e) {
rate(e.source.i);
});
}
使用上面的逻辑方法,您应该能够更改 max
来设置您想要的星星数量,并简单地设置 rate(v)
来执行以下操作之一上面评论中的两个选项。请记住,您需要使用 left
或某种类型的定位将 stars[i]
添加到 View 中,该定位会根据可用星星的数量递增。希望这会有所帮助。
关于iphone - 如何使用 Titanium 为 iPhone 应用程序创建星级评级控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6093299/