java - 在网页中搜索..Android

标签 java android search web-applications

我需要在网页中搜索..我开发了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);    
            }
       });
    }

最佳答案

首先,您需要将 url 加载到您的 WebView 中.然后使用 javascript in your Webview寻找。您可以使用此 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, "\|"));
    });
});

关于java - 在网页中搜索..Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899957/

相关文章:

java - Android 应用程序开发中的 SQLite 数据库行为

c++ - C++中的字符串比较

java - 如何通过 ContentProvider 向 Facebook 提供图像?

android - 带有 LayoutInflater 的无法访问的语句

java - Guice配置错误

android - 需要使用闹钟管理器定期重启安卓设备

search - lisp 哈希符号替换返回值

php - 按前缀在数据库表中搜索字符串

java - 设置由 Activity 链接的新页面

Java Scanner指定行分隔符