展会信息港展会大全

在Android应用中实现Google搜索的例子
来源:互联网   发布日期:2015-09-24 20:33:37   浏览:2833次  

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

有一个很简单的方法在你的 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:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">

<edittext android:id="@+id/editTextInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter search text">

<requestfocus>

</requestfocus></edittext>

<button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Search" android:layout_gravity="center" android:onclick="onSearchClick" android:layout_margintop="10dp">

</button></linearlayout>

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

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

</uses-permission>

输出:

Android - Google search intent

\

赞助本站

人工智能实验室

相关热词: Google搜索

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港