有一个很简单的方法在你的 Android 应用中实现 Google 搜索。在这个例子中,我们将接受用户的输入作为搜索词,我们将使用到 Intent.ACTION_WEB_SEARCH 。

GoogleSearchIntentActivity.java

 

package com.technotalkative.googlesearchintent;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class GoogleSearchIntentActivity extends Activity {

 private EditText editTextInput;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editTextInput = (EditText) findViewById(R.id.editTextInput);
   }

    public void onSearchClick(View v)
    {
     try {
       Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
             String term = editTextInput.getText().toString();
             intent.putExtra(SearchManager.QUERY, term);
             startActivity(intent);
  } catch (Exception e) {
   // TODO: handle exception
  }

    }
}

 

main.xml

 

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:andro>

    <edittext android:>

        <requestfocus>
    </requestfocus></edittext>

    <button android:>

</button></linearlayout>

 

注意:不要忘了在 AndroidManifest.xml 文件中添加 INTERNET 权限。

 

 <uses-permission android:name="android.permission.INTERNET">
</uses-permission>

 

输出:

在Android应用中实现Google搜索的例子

在Android应用中实现Google搜索的例子

相关文章:

  • 2021-09-21
  • 2021-11-14
  • 2021-11-12
  • 2021-11-04
  • 2021-11-14
  • 2022-12-23
  • 2021-09-18
猜你喜欢
  • 2021-12-23
  • 2021-11-18
  • 2021-06-07
  • 2022-01-29
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案