我想在按下向上箭頭時關注TextField的結尾。我使用的是:AS3:setSelection向上箭頭覆蓋
txt.setSelection(txt.text.length,txt.text.length);
這對於除向上箭頭任意鍵的偉大工程。我相信,當向上箭頭對焦時,它會自動將選擇設置爲TextField的開頭。我如何覆蓋這種默認行爲?
我想在按下向上箭頭時關注TextField的結尾。我使用的是:AS3:setSelection向上箭頭覆蓋
txt.setSelection(txt.text.length,txt.text.length);
這對於除向上箭頭任意鍵的偉大工程。我相信,當向上箭頭對焦時,它會自動將選擇設置爲TextField的開頭。我如何覆蓋這種默認行爲?
我想改變Home鍵的行爲,這是我做的:
(下面的代碼基本上應禁用HOME鍵,但可以進行修改,以使其做任何事情)
// Create two variables two remember the TextField's selection
// so that it can be restored later. These varaibles correspong
// to TextField.selectionBeginIndex and TextField.selectionEndIndex
var overrideSelectionBeginIndex:int = -1;
var overrideSelectionEndIndex:int;
// Create a KEY_DOWN listener to intercept the event ->
// (Assuming that you have a TextField named 'input')
input.addEventListener(KeyboardEvent.KEY_DOWN, event_inputKeyDown, false, 0, true);
function event_inputKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.HOME){
if(overrideSelectionBeginIndex == -1){
stage.addEventListener(Event.RENDER, event_inputOverrideKeyDown, false, 0, true);
stage.invalidate();
}
// At this point the variables 'overrideSelectionBeginIndex'
// and 'overrideSelectionEndIndex' could be set to whatever
// you want but for this example they just store the
// input's selection before the home key changes it.
overrideSelectionBeginIndex = input.selectionBeginIndex;
overrideSelectionEndIndex = input.selectionEndIndex;
}
}
// Create a function that will be called after the key is
// pressed to override it's behavior
function event_inputOverrideKeyDown(event:Event):void{
// Restore the selection
input.setSelection(overrideSelectionBeginIndex, overrideSelectionEndIndex);
// Clean up
stage.removeEventListener(Event.RENDER, event_inputOverrideKeyDown);
overrideSelectionBeginIndex = -1;
overrideSelectionEndIndex = -1;
}
有可以應用到行動,它取消(我假定這將是)的Prevent Default (livedocs)功能,否則,你可以嘗試用stopPropagation,而不是抓住它:
此處理不當進行了測試,而應該看是這樣的:
function buttonPress(ev:KeyboardEvent):void{
txt.setSelection(txt.text.length,txt.text.length);
ev.preventDefault();
}
我今天發佈了一個類似的答案,但我已經刪除了它。在這種情況下`preventDefault()`方法不起作用。 `stopPropagation()`也不起作用。我測試了他們兩個。順便說一下,有一次類似的問題:http://stackoverflow.com/questions/1018259/how-do-you-prevent-arrow-up-down-default-behaviour-in-a-textfield和OP有還說他試過`preventDefault()`和`stopImmediatePropagation()`,但都沒有爲他工作(我只是不明白他爲什麼接受答案,因爲它仍然不適合他 - 對我也是如此) 。 – rhino 2011-02-08 16:18:11
有沒有解決方法? – Abdulla 2011-02-08 22:43:24