自动完成搜索句子中的任何单词

时间:2015-05-28 04:22:29

标签: actionscript-3 autocomplete flash-cs6

我正在为我的Flash应用使用自动填充功能。自动完成功能使用外部文本文件。 当我输入句子的第一个单词时,它会显示以该单词开头的所有句子。

是否可以显示所有包含此词的句子(而不仅仅是句子的开头)?

例如: 我有两个短语:"我要去学校"和"我要去找他"。

我希望能够输入" school"并显示第一句话。

你知道我怎么做吗? 现在,我必须输入"我要去s"为了显示第一句话。

这是我的代码:

urlLoader.load(new URLRequest("test.txt"));
            urlLoader.addEventListener(Event.COMPLETE, loadComplete);
            inputField.addEventListener(KeyboardEvent.KEY_UP, suggest);

 function loadComplete(e:Event):void
        {
            suggestions = e.target.data.split(",");
        }

         function suggest(e:KeyboardEvent):void
        {
            suggested = [];

            for (var i:int = 0; i < textfields.length; i++)
            {
                removeChild(textfields[i]);
            }

            textfields = [];

            for (var j:int = 0; j < suggestions.length; j++)
            {
                if (suggestions[j].indexOf(inputField.text.toLowerCase()) == 0)
                {
                    var term:TextField = new TextField();
                    term.width = 300;
                    term.height = 20;
                    term.x = 70;
                    term.y = (20 * suggested.length) + 314;
                    term.border = true;
                    term.borderColor = 0x353535;
                    term.background = true;
                    term.backgroundColor = 0xFF9900;
                    term.textColor = 0x4C311D;
                    term.defaultTextFormat = format;

                    term.addEventListener(MouseEvent.MOUSE_UP, useWord);
                    term.addEventListener(MouseEvent.MOUSE_OVER, hover);
                    term.addEventListener(MouseEvent.MOUSE_OUT, out);
                    term.addEventListener(MouseEvent.CLICK, tellMe);

                    addChild(term);
                    textfields.push(term);

                    suggested.push(suggestions[j]);

                    term.text = suggestions[j];
                }

            }

            if (inputField.length == 0)
            {
                suggested = [];

                for (var k:int = 0; k < textfields.length; k++)
                {
                    removeChild(textfields[k]);
                }

                textfields = [];
            }

            if(e.keyCode == Keyboard.DOWN && currentSelection < textfields.length-1)
            {
                currentSelection++;
                textfields[currentSelection].textColor = 0x4C311D;
            }

            if(e.keyCode == Keyboard.UP && currentSelection > 0)
            {
                currentSelection--;
                textfields[currentSelection].textColor = 0x4C311D;
            }

            if(e.keyCode == Keyboard.ENTER)
            {
                inputField.text = textfields[currentSelection].text;

                suggested = [];

                for (var l:int = 0; l < textfields.length; l++)
                {
                    removeChild(textfields[l]);
                }

                textfields = [];
                currentSelection = 0;
            }
        }

         function useWord(e:MouseEvent):void
        {
            inputField.text = e.target.text;

            suggested = [];

            for (var i:int = 0; i < textfields.length; i++)
            {
                removeChild(textfields[i]);
            }

            textfields = [];
        } 

谢谢

1 个答案:

答案 0 :(得分:1)

更改条件:

package com.samepinch.controllers;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.samepinch.services.UserService;
import com.samepinch.utills.response.ResponseHandler;

@RestController
@RequestMapping("/api/v1")
public class UserController {
    public static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    /**
     * to pass user info to service
     */
    /*@RequestMapping(value = "/signup",method = RequestMethod.POST)
    public Map<String, Object> saveUser(@RequestBody User user) {
successfully", HttpStatus.ACCEPTED, false, null);
    }*/

    /**
     * to delete user by id
     */
    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("id") String id) {
        userService.deleteUser(id);
    }

    /**
     * to update user by id
     */
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public String updateUser() {
        System.out.println("in update method");
//      userService.updateUser(user);
        return "success";
    }

    @RequestMapping(value = "/user" , method = RequestMethod.GET)
    public Map<String,Object> getUsers(){
        log.info("getting user");
        return ResponseHandler.generateResponse("", HttpStatus.ACCEPTED, false, null);
    }

}       userService.saveUser(user);
        return ResponseHandler.generateResponse("User registerted successfully", HttpStatus.ACCEPTED, false, null);
    }*/

    /**
     * to delete user by id
     */
    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("id") String id) {
        userService.deleteUser(id);
    }

    /**
     * to update user by id
     */
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public String updateUser() {
        System.out.println("in update method");
//      userService.updateUser(user);
        return "success";
    }

    @RequestMapping(value = "/user" , method = RequestMethod.GET)
    public Map<String,Object> getUsers(){
        log.info("getting user");
        return ResponseHandler.generateResponse("", HttpStatus.ACCEPTED, false, null);
    }

}

suggestions[j].indexOf(inputField.text.toLowerCase()) == 0