本文介绍了当onSearchRequested被称为未激活搜索对话框(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

初​​学者工作的一个Android应用程序,我想,当onSearchRequested方法是在主要活动按钮按下后调用来启动一个可搜索的活动。目前,当按钮为pressed,没有任何反应。

下面是MainActivity.Java:

 包com.example.activity2;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;公共类MainActivity扩展ActionBarActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        button1.setOnClickListener(新View.OnClickListener()
        {            @覆盖
            公共无效的onClick(视图v)
            {
                onSearchRequested();
            }
        });        }    / **当用户点击发送按钮调用* /
  //公共无效的sendMessage(查看视图)
   // {  //}    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }
}

下面是清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.activity2
    安卓版code =1
    机器人:=的versionName1.0>    <使用许可权
         机器人:名字=android.permission.INTERNET对>< /使用许可权>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =19/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.SEARCH/>
                <作用机器人:名字=android.intent.action.MAIN/>
                <作用机器人:名字=android.intent.action.FLAG_ACTIVITY_NEW_TASK/>
                &所述;元数据
                机器人:名字=android.app.default_searchable
                机器人:值=com.example.activity2.SearchActivity/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=com.authorwjf.youtubeapi.MainActivity
            机器人:标签=@字符串/ title_activity_activity2>
        < /活性GT;
        <活动机器人:名字=。SearchActivity>
            机器人:launchMode =singleTop>
            &所述;意图滤光器>
                <作用机器人:名字=com.example.activity2.SEARCHACTIVITY/>                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>        < /活性GT;
        <活动
            机器人:OtherActivityNAME =
            机器人:标签=@字符串/ title_activity_other>            <! - 使搜索对话框发送搜索,SearchableActivity - >
            &所述;元数据
                机器人:名字=android.app.default_searchable
                SearchActivity:机器人值= />
        < /活性GT;
        <活动
            机器人:名字=。SearchListView
            机器人:标签=@字符串/ title_activity_search_list_view>
        < /活性GT;
        <活动
            机器人:名字=。SearchYoutube
            机器人:标签=@字符串/ title_activity_search_youtube>
        < /活性GT;
        &所述;元数据
                机器人:名字=android.app.searchable
                机器人:资源=@ XML /搜索/>
    < /用途>< /清单>

SearchActivity.Java

 包com.example.activity2;
进口的java.util.ArrayList;
进口java.util.Iterator的;进口android.app.Activity;
进口android.app.SearchManager;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;进口com.google.api.services.youtube.model.SearchResult;公共类SearchActivity延伸活动{
    ListView的LST;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_search_activity);
        意向意图= getIntent();
        如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
          查询字符串= intent.getStringExtra(SearchManager.QUERY);
          ArrayList的<&SearchResult中GT;结果=新的ArrayList<&SearchResult中GT;();
          迭代器<&SearchResult中GT;它= results.iterator();
          SearchYoutube prettyPrint(它,查询)。
          LST =(ListView控件)findViewById(R.id.list);
          ArrayAdapter<串GT;适配器=新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,SearchYoutube.ytstuff);
          lst.setAdapter(适配器);
    }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.search,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }
}


解决方案

您需要实现您的搜索活动。

 公共类SearchableActivity扩展ListActivity {    公共无效的onCreate(捆绑savedInstanceState){
          super.onCreate(savedInstanceState);
          Log.d(搜索,在这里);
          handleIntent(getIntent());
       }       公共无效onNewIntent(意向意图){
          setIntent(意向);
          handleIntent(意向);
       }       公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
          //调用相应活动细节
       }       私人无效handleIntent(意向意图){
          如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
             查询字符串= intent.getStringExtra(SearchManager.QUERY);
             doSearch(查询);
          }
       }       私人无效doSearch(字符串queryStr){
           Toast.makeText(getApplicationContext(),queryStr,Toast.LENGTH_LONG).show();
       }
}

而在你的Andr​​oidManifest.xml中指定的元数据

 <元数据
    机器人:名字=android.app.default_searchable
    SearchableActivity:机器人值= /><活动
    机器人:名字=。SearchableActivity
    机器人:标签=@字符串/ APP_NAME
    机器人:launchMode =singleTop>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.SEARCH/>
    &所述; /意图滤光器>    &所述;元数据
        机器人:名字=android.app.searchable
        机器人:资源=@ XML /搜索/>
< /活性GT;

Beginner working on an android app and I'm trying to start a searchable activity when the onSearchRequested method is called after a button push in the main activity. Currently, when the button is pressed, nothing happens.

Here's MainActivity.Java:

package com.example.activity2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;



public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                onSearchRequested();
            }
        });

        }

    /** Called when the user clicks the Send button */
  //  public void sendMessage(View view)
   // {

  //  }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activity2"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.FLAG_ACTIVITY_NEW_TASK" />
                <meta-data
                android:name="android.app.default_searchable"
                android:value="com.example.activity2.SearchActivity" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.authorwjf.youtubeapi.MainActivity"
            android:label="@string/title_activity_activity2" >
        </activity>
        <activity android:name=".SearchActivity" >
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="com.example.activity2.SEARCHACTIVITY" />

                <category android:name="android.intent.category.DEFAULT"  />
            </intent-filter>

        </activity>
        <activity
            android:name=".OtherActivity"
            android:label="@string/title_activity_other" >

            <!-- enable the search dialog to send searches to SearchableActivity -->
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchActivity" />
        </activity>
        <activity
            android:name=".SearchListView"
            android:label="@string/title_activity_search_list_view" >
        </activity>
        <activity
            android:name=".SearchYoutube"
            android:label="@string/title_activity_search_youtube" >
        </activity>
        <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
    </application>

</manifest>

SearchActivity.Java

package com.example.activity2;


import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.api.services.youtube.model.SearchResult;

public class SearchActivity extends Activity {
    ListView lst;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_activity);
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          ArrayList<SearchResult> results = new ArrayList<SearchResult>();
          Iterator<SearchResult> it = results.iterator();
          SearchYoutube.prettyPrint(it, query);
          lst = (ListView) findViewById(R.id.list);
          ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, SearchYoutube.ytstuff);
          lst.setAdapter(adapter);
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
解决方案

You need to implement your search activity.

public class SearchableActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.d("SEARCH", "HERE");
          handleIntent(getIntent());
       }

       public void onNewIntent(Intent intent) {
          setIntent(intent);
          handleIntent(intent);
       }

       public void onListItemClick(ListView l, View v, int position, long id) {
          // call the appropriate detail activity
       }

       private void handleIntent(Intent intent) {
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
             String query = intent.getStringExtra(SearchManager.QUERY);
             doSearch(query);
          }
       }

       private void doSearch(String queryStr) {
           Toast.makeText(getApplicationContext(), queryStr, Toast.LENGTH_LONG).show();
       }
}

And specify the meta-data in your AndroidManifest.xml

<meta-data
    android:name="android.app.default_searchable"
    android:value=".SearchableActivity" />

<activity
    android:name=".SearchableActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

这篇关于当onSearchRequested被称为未激活搜索对话框(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:40