简体   繁体   English

Android Google和Yahoo从EditText搜索

[英]Android Google and Yahoo search from EditText

I am trying to create a program with one EditText, two buttons (Google and Yahoo), and one WebView. 我试图用一个EditText,两个按钮(Google和Yahoo)和一个WebView创建一个程序。 What I am trying to create is to search the word typed in EditText. 我正在尝试创建的是搜索EditText中键入的单词。 If the user clicks Google, the text in the EditText will automatically be searched in Google.com, while if the user clicks Yahoo, the text in the EditText will automatically be searched in Yahoo.com. 如果用户单击Google,则将在Google.com中自动搜索EditText中的文本,而如果用户单击Yahoo,则将在Yahoo.com中自动搜索EditText中的文本。 So far Google is already working. 到目前为止,谷歌已经在工作。 Can anyone help me with Yahoo. 谁能帮助我与Yahoo。 Thank You 谢谢

package com.example.webbrowser3;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class WebBrowser3 extends Activity implements OnClickListener {
    Button google;
    Button yahoo;
    WebView WebView;
    EditText search;
    String url;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_browser3);
        WebView = (WebView)findViewById(R.id.webview);
        search = (EditText)findViewById(R.id.search);
        google = (Button)findViewById(R.id.google);
        google.setOnClickListener(this);
        yahoo = (Button)findViewById(R.id.yahoo);
        yahoo.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId()==R.id.google)
        {
            intent = new Intent(Intent.ACTION_WEB_SEARCH);
            url=search.getText().toString();
            intent.putExtra(SearchManager.QUERY, url);
            startActivity(intent);
        }
        else if (v.getId()==R.id.yahoo)
        {
            url=search.getText().toString();
            WebView.loadUrl("http://www.yahoo.com" + url);
        }


    }

}

With the way you are doing it, you need URL encode the EditText's text and prepend "/search?p=". 用这种方式,您需要对EditText的文本进行URL编码,并在其前面加上“ / search?p =“。

Here is a URL Encoder: http://meyerweb.com/eric/tools/dencoder/ 这是一个URL编码器: http : //meyerweb.com/eric/tools/dencoder/

You should do a Yahoo Search and see what kind of URL it makes with your search query and build your URL as so. 您应该执行Yahoo Search,并查看它与搜索查询一起生成的URL的类型,并以此构建URL。 In order to determine how to do a Bing search, you would see what Bing does to your search query and try to re-create that URL. 为了确定如何执行Bing搜索,您将看到Bing对搜索查询的作用,然后尝试重新创建该URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM