1
        itemBox.addKeyUpHandler( new KeyUpHandler()
        {
            public void onKeyUp( KeyUpEvent event )
            {
                String currentValue = itemBox.getValue().trim();
                // handle backspace
                if( event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE )
                {
                    if( "".equals( currentValue ) )
                    {
                        doTheJob( );
                    }
                }
            }
        } );

予想される動作: テキストボックスが空の場合、削除を押すと doTheJob(); が実行されます。

現在の動作: 1 つの文字がある場合、削除を押すと、doTheJob(); がトリガーされます。

つまり、削除キーを押す前にテキストボックスの内容を取得する方法はありますか? var を使用して最後の値を保持しようとしましたが、別のリスナーを登録する必要があり、impl はあまり効果的ではありません。

ご意見ありがとうございます。

////////////////////編集 //////////////////////

KeyDownHandler を使用すると上記の問題は解決しましたが、別の問題が発生しました。テキストボックスをクリアしますが、そこには常にコンマがあります。

        itemBox.addKeyDownHandler( new KeyDownHandler()
        {
            public void onKeyDown( KeyDownEvent event )
            {
                // handle backspace
                if( event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE )
                {
                    String currentValue = itemBox.getValue().trim();
                    if( "".equals( currentValue ) )
                    {
                       doTheJob();
                    }
                }
                // handle comma
                else if( event.getNativeKeyCode() == 188 )
                {
                     doOtherJob();
                    //clear TextBox for new input
                     itemBox.setValue( "" );
                     itemBox.setFocus( setFocus );
                }
            }
        } );
4

1 に答える 1

1

itemBox.setFocus( setFocus );

イベントのバブリングを防ぐ

event.preventDefault();
event.stopPropagation();

そのため、テキストボックスの内容にコンマが追加される前にイベントがキャンセルされます。

于 2012-07-25T11:45:03.110 に答える