Skip to content

Instantly share code, notes, and snippets.

@nuno
Forked from exclusiveTanim/app.js
Created July 6, 2014 01:28
Show Gist options
  • Save nuno/f1e5c7baa146302700dc to your computer and use it in GitHub Desktop.
Save nuno/f1e5c7baa146302700dc to your computer and use it in GitHub Desktop.
Change the button states(Android & iOS)
/*
Hello, I have tested this issue in Ti SDK 3.3.0.RC. Its working good.
Testing Environment:
Titanium SDK: 3.3.0.RC, 3.2.3.GA
Titanium CLI: 3.2.3
IOS Simulator 7.1
Appcelerator Studio, build: 3.3.0.201406271159
*/
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
var currentView = Ti.UI.createView({
backgroundColor : '#EFEFEF'
});
var button = [], top = 0;
for (var i = 0; i < 3; i++) {
top += 50;
//Creating each button
button[i] = Titanium.UI.createButton({
color : 'red',
top : top,
width : '80%',
value : 1
});
button[i].title = 'State ' + button[i].value;
button[i].addEventListener('click', changeState);
//Adding the buttons to the center view
currentView.add(button[i]);
}
var buttonState = Titanium.UI.createButton({
color : 'red',
top : top + 50,
title : 'View button states',
width : '80%',
});
var lblStates = Titanium.UI.createLabel({
color : 'red',
layout : 'horizontal',
top : top + 100,
text : 'Click on show button to view the button states',
width : '80%',
});
buttonState.addEventListener('click', showButtonStates);
currentView.add(lblStates);
currentView.add(buttonState);
win.add(currentView);
win.open();
//Changing the state of the clicked button
function changeState(e) {
e.source.value = 2;
e.source.color = 'blue';
e.source.title = 'State ' + e.source.value;
for (var i = 0; i < 3; i++) {
if (e.source !== button[i]) {
button[i].value = 1;
button[i].color = 'red';
button[i].title = 'State ' + button[i].value;
}
}
}
//To display the button state
function showButtonStates() {
lblStates.text = "";
for (var i = 0; i < 3; i++) {
lblStates.text = lblStates.text + '\nbutton' + (i + 1) + ' ---> state: ' + button[i].value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment