0

私は一連のボタンを持っています。

各ボタンには 2 つの異なる状態があります: 最初の状態、2 番目の状態です。ボタンは、クリックするたびに交互の状態に変わります。

あるシナリオでは、Button1 をクリックすると、Second 状態になります。次に、Button2 をクリックすると、Button2 は Second State になり、Button1 (または Second State にあるその他のボタン) は First State に戻ります。

Appcelerator Titanium でこれを行うにはどうすればよいですか?

このようなボタンを作成しました

function createButtons(data){

    for (var i = 0; i < data.length; i++){
        //Creating each button
        var button  = Titanium.UI.createImageView({
            image:  data[i].path,
            value: 1
        });

        //Adding the buttons to the center view
        centerButtons.add(button);
    }
}

クリックするたびvalueに、ボタンの状態を 1 または 2 に変更して、ボタンの状態を識別しています。問題は、たとえば、Button1 をクリックすると値を変更できますが、方法がわかりません。最初の状態にリセットできるように、他のどのボタンが既に 2 番目の状態にあるかを検出しますか?

4

2 に答える 2

3

次のサンプル コードは、単純に作業を行います。ここでは、imageView の代わりにボタンを使用しています。それを使用してコードを変更できます。

var win = Ti.UI.createWindow({
    backgroundColor : 'white'
});
var currentView = Ti.UI.createView({
    backgroundColor : '#EFEFEF'
});
var button = [],top = 0;
for (var i = 0; i < 5; i++){
    top += 80;
    //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 + 80,
    title  : 'View button states',
    width : '80%',
});

var lblStates  = Titanium.UI.createLabel({
    color  : 'red',
    layout: 'horizontal',
    top    : top + 160,
    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.title  = 'State ' + e.source.value; 
    for(var i = 0;i<5;i++){
        if(e.source !== button[i]){
            button[i].value = 1;
            button[i].title  = 'State ' + button[i].value; 
        }
    }    
}
//To display the button state
function showButtonStates(){
    lblStates.text = "";
    for(var i =0;i<5;i++){
        lblStates.text = lblStates.text + '\nbutton' + (i+1) + ' ---> state: ' + button[i].value; 
    }
}
于 2013-03-14T05:46:53.353 に答える
0
  • 何かがクリックされるたびにすべてのボタンをリセットして元の状態に戻し、新しいボタンの新しい状態を設定します...
  • ... または、最後に変更されたボタン (変数?! それらは何ですか?) を追跡し、新しいボタンの新しい状態を設定する前に元に戻します。
于 2013-03-13T19:00:01.183 に答える