如何重置Flash中的当前选项卡索引?

时间:2010-11-29 14:25:56

标签: flash actionscript-3

例如,如果我有一组有序的对象[a, b, c, d, e]并正确分配了tabIndex属性,并且焦点当前位于对象c上,我想更改焦点,以便:

a)没有什么是重点;

b)下次按Tab键时,对象a会聚焦。

stage.focus = null解决了(a),但它会记住当按下制表符时当前索引导致对象d被聚焦。

4 个答案:

答案 0 :(得分:1)

您可以将剩余对象的tabEnabled属性切换为false,而不是尝试更改索引,当焦点位于第一个对象上时,再次启用它。

这个例子不是在函数中构造的,它只是演示代码来阐明我的意思

    var objects:Array = [a , b , c , d , e];

    //set a KeyboardEvent listener &
    //increment the currentIndex value when TAB is pressed
    var currentIndex:int = -1;

    //when you need to reset , disable tabEnabled property
    //for remaining objects & reset the currentIndex
    for( var i:int ; i < objects.length ; ++i )
         if( i > currentIndex )
           objects[i].tabEnabled = false;
    currentIndex = -1;
    stage.focus = null;

再次按Tab键,焦点位于

     //enable objects
    for( var i:int ; i < objects.length ; ++i )
           objects[i].tabEnabled = true;

答案 1 :(得分:0)

最简单的方法是将焦点设置为e,然后设置为null

答案 2 :(得分:0)

我不确定这是解决问题的最佳方法,但它绝对有效。基本上,您可以跟踪焦点的UI元素。然后在使焦点归零之前,存储当前处于焦点的元素。看看 - 我用一些TextInputs和一个Button测试了这个:

// you should change the wildcard datatype to match your UI elements
var currentFocus:*;
var lastFocus:*;

btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(evt:MouseEvent):void{

    // store the element currently in focus
    // so that we can re-select it next time tab is hit
    lastFocus = currentFocus;
    // setting stage.focus = null will lost keyboard focus
    // so we set it to stage
    stage.focus=stage
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyReleased);
function onKeyReleased(evt:KeyboardEvent):void{

    if (evt.keyCode == Keyboard.TAB){

    // if lastFocus exists, set the stage focus accordingly
    // update currentFocus and then set lastFocus to null
      if (lastFocus){
        stage.focus = lastFocus;
        currentFocus = lastFocus;
        lastFocus = null;
      } else {
        // keep track of current focus
        currentFocus = stage.focus;
      }
    }
}

答案 3 :(得分:0)

This solution doesn't require altering the tabIndex of the items in the tab loop.

Then add the following code to your project:

import flash.events.FocusEvent;

var tab_resetter:MovieClip = new MovieClip();
tab_resetter.tabIndex = 9999;
tab_resetter.addEventListener( FocusEvent.FOCUS_OUT, function() {
    stage.removeChild( tab_resetter );
});

function resetTabIndex():void {
    stage.addChild( tab_resetter );
    stage.focus = tab_resetter;
}

Since tab_resetter is not on the stage most of the time, it won't be part of the normal tab loop. Once you call resetTabIndex(), tab_resetter is inserted into the tab loop and given focus. If you give tab_resetter a sufficiently high tabIndex value, hitting tab after calling resetTabIndex() will give focus to the first item in the tab loop.