1

我需要在网页中搜索..我开发和 android 应用程序..当用户按下按钮时,网页将被打开,但我需要用户输入关键字,当用户按下按钮时,该关键字必须在网页内搜索..我在这里提供我的 coe,你能建议我接下来要做的事情吗

Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button) findViewById(R.id.button1);
        ev = (EditText) findViewById(R.id.editText1);

        btn1.setOnClickListener(new View.OnClickListener() 
        {


            @Override
            public void onClick(View v) 

            {
                 Intent intent = new Intent(Intent.ACTION_VIEW,
                 Uri.parse("http://www.androidaspect.com"));

                 startActivity(intent);    
            }
       });
    }
4

1 回答 1

1

首先,您需要将 url 加载到您的 WebView中。然后 在您的 Webview 中使用 javascript进行搜索。您可以使用此 javascript 进行搜索

$(document).ready(function() {
    function trim(value) {
        var temp = value;
        var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
        if (obj.test(temp)) {
            temp = temp.replace(obj, '$2');
        }
        var obj = / +/g;
        temp = temp.replace(obj, " ");
        if (temp == " ") {
            temp = "";
        }
        return temp;
    }
    var body = $('body');

    SearchQueue("of Micro");

    function SearchQueue(text) {
        if (text !== null) {
            text = text.replace(/^\s*$[\n\r]{1,}/gm, ''); // Removing empty line breaks
            text = text.replace(/”/g, "\"");
            text = text.replace(/“/g, "\"");
            text = text.replace(/”/g, "\"");
            text = text.replace(/’/g, "\'");
            text = text.replace(/‘/g, "\'");
            text = text.replace(/–/g, "\-");
            text = text.replace(/—/g, "\-");
            text = text.replace(/–/g, "\-");
            var txt1 = text;
            text = trim(txt1);
            var SearchItems = text.split(/\r\n|\r|\n/);
            var replaced = body.text();
            for (var i = 0; i < SearchItems.length; i++) {
                var tempRep = '<span class="highlight" style="background-color: yellow">';
                tempRep = tempRep + SearchItems[i];
                tempRep = tempRep + '</span>';
                replaced = replaced.replace(SearchItems[i], tempRep);
            }
            $("body").html(replaced);
        }
    }
    shortcut.add("Ctrl+Z", function() {
        $('.highlight').toggleClass();
    });

    shortcut.add("Ctrl+V", function() {
        var txt = window.clipboardData.getData("Text");
        var ClipText = txt.replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/, "$1");
        SearchQueue(ClipText.replace(/\%20|\+/g, "\|"));
    });
});
于 2013-04-09T10:59:11.697 回答