Android的 - 如何禁用搜索按钮,如何实现onSearchRequested()? [英] Android - How to disable Search button, how to implement onSearchRequested()?

查看:122
本文介绍了Android的 - 如何禁用搜索按钮,如何实现onSearchRequested()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过了以下工作: <一href="http://stackoverflow.com/questions/5215541/how-to-have-clicking-the-phones-search-button-do-nothing">How有点击手机上的搜索按钮做什么?

不过,这并不为我工作。任何人有什么建议?谢谢。 继菲尔的建议,根据他的回答更新code。 显示错误 http://www.youtube.com/watch?v= 9lekcH1JAf0和放大器;配备= youtu.be

所以,我做了一个样本项目。我曾在两个不同的手机,Android版本2.3.5和4.1测试此,我还测试了这款在模拟器版本2.2。当我点击按钮显示对话框,它显示进度对话框蛮好的,就应该继续这样做。但点击手机上的搜索按钮和仿真器,使进度对话框消失。这里的类,并且表现如下。

目前的行为是单击搜索按钮时进度对话框消失。 预期或需要的行为是禁止的搜索按钮,或类似的内容显示的对话框,单击搜索按钮时,此点击不会使对话框消失。

 包com.example.test6;

进口android.os.Bundle;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类MainActivity延伸活动{

    按钮showDialogBu​​tton;
    上下文语境;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        showDialogBu​​tton =(按钮)findViewById(R.id.showDialogBu​​tton);
        showDialogBu​​tton.setOnClickListener(的ShowDialog);
        上下文=这一点;
        开始搜索(空,假,空,假);
    }

    私人OnClickListener的ShowDialog =新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            ProgressDialog progressDialog =新ProgressDialog(上下文);
            progressDialog.show();
        }
    };

    @覆盖
    公共布尔onSearchRequested(){
       Log.d(onSearchRequested,搜索键pressed);
       返回false;
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}

&LT;舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.test6
    安卓版code =1
    机器人:VERSIONNAME =1.0&GT;

    &LT;用途-SDK
        安卓的minSdkVersion =1
        机器人:targetSdkVersion =15/&GT;

    &lt;应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名称=。MainActivity
            机器人:标签=TEST6&GT;
            &LT;意向滤光器&gt;
                &lt;作用机器人:名称=android.intent.action.MAIN/&GT;

                &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT;意向滤光器&gt;
            &lt;作用机器人:名称=android.intent.action.SEARCH/&GT;
            &LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
        &所述; /意图滤光器&gt;
        &所述;元数据
            机器人:名称=android.app.searchable
            机器人:资源=@ XML /搜索/&GT;
        &LT; /活性GT;
    &LT; /用途&gt;

&LT; /舱单&GT;
 

我的搜索XML是:RES / XML / searchable.xml根据提供的参考。这是我的searchable.xml是这样的:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;可搜索的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:searchSuggestAuthority =字典
    机器人:searchSuggestIntentAction =android.intent.action.VIEW
    机器人:includeInGlobalSearch =真正的&GT;
&LT; /搜索&GT;
 

解决方案

修改

很高兴我下面的code是有益的。随着更新的问题(以及视频),我现在相信这是你需要的答案:<一href="http://stackoverflow.com/questions/2502443/$p$pvent-progressdialog-from-being-dismissed-when-i-click-the-search-button-andr">$p$pvent ProgressDialog从当我点击搜索按钮(安卓)被解雇

- pre-EDIT 的 -

而不是调用的 onSearchRequested()的onCreate(),你需要调用:

 开始搜索(空,假,空,假);
 

随后,以prevent从使用 onSearchRequested()方法搜索按钮,你重写它并返回false:

  @覆盖
公共布尔onSearchRequested()
{
   返回false;
}
 

该解决方案是直接在 onSearchRequested文档讨论< /一>:

  

您可以使用此功能作为一种简单的方式来启动搜索的用户界面,在   回应中的菜单项,搜索按钮或其他部件的   活动。除非overidden,调用此函数是一样的   调用开始搜索(空,假,空,假的),这将启动搜索   对于在其清单中指定的当前活动,见   SearchManager。

     

您可以覆盖此函数强制全局搜索,例如:在   响应于一个专门的搜索关键,或者完全屏蔽搜索(由   简单地返回false)。

的Andr​​oidManifest.xml 也是不完整的。你需要证明你的活动的搜索。改变你的 MainActivity 的声明如下:

 &LT;活动
        机器人:名称=。MainActivity
        机器人:标签=@字符串/ title_activity_main&GT;
        &LT;意向滤光器&gt;
            &lt;作用机器人:名称=android.intent.action.MAIN/&GT;
            &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
        &所述; /意图滤光器&gt;
        &LT;意向滤光器&gt;
            &lt;作用机器人:名称=android.intent.action.SEARCH/&GT;
            &LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
        &所述; /意图滤光器&gt;
        &所述;元数据
            机器人:名称=android.app.searchable
            机器人:资源=@ XML /搜索/&GT;
    &LT; /活性GT;
 

注意的 @ XML /搜索的是定义如何寻找你的apk文件的XML参考。这种资源进行了详细讨论 href="http://developer.android.com/guide/topics/search/searchable-config.html" rel="nofollow">。

Checked out the following: How to have clicking the phone's search button do nothing?

But it does not work for me. Anybody has any other suggestions? Thank you. Following Phil's suggestion updating the code according to his answer. Shows the bug http://www.youtube.com/watch?v=9lekcH1JAf0&feature=youtu.be

So I have made a sample project. I have tested this on two different phones, Android version 2.3.5 and 4.1, I also tested this on emulator version 2.2. After I click the button show dialog, it shows the progress dialog just fine, and it should continue to do so. But clicking the Search button on the phones and the emulator makes the progress dialog disappear. Here's the class, and manifest follows.

Current behavior is that when the search button is clicked the progress dialog disappears. Expected or needed behavior is to disable the search button, or similarly when the dialog is being shown and the search button is clicked, this click does not make the dialog disappear.

    package com.example.test6;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button showDialogButton;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialogButton = (Button) findViewById(R.id.showDialogButton);
        showDialogButton.setOnClickListener(showDialog);
        context = this;
        startSearch(null, false, null, false);
    }

    private OnClickListener showDialog = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ProgressDialog progressDialog = new ProgressDialog(context);
            progressDialog.show();
        }
    };

    @Override
    public boolean onSearchRequested() {
       Log.d("onSearchRequested", "search key pressed");
       return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test6"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="1"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="Test6" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

My searchable xml is at: res/xml/searchable.xml according to the reference provided. This is how my searchable.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:searchSuggestAuthority="dictionary"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:includeInGlobalSearch="true">
</searchable>

解决方案

EDIT

Glad my code below was helpful. With your updated question (along with the video), I now believe this is the answer you need: Prevent ProgressDialog from being dismissed when I click the search button (Android)

--PRE-EDIT--

Instead of calling onSearchRequested() from onCreate(), you need to call:

startSearch(null, false, null, false);

Then, to prevent the search button from using the onSearchRequested() method, you override it and return false:

@Override
public boolean onSearchRequested()
{
   return false;
}

This solution is directly discussed in the onSearchRequested documentation:

You can use this function as a simple way to launch the search UI, in response to a menu item, search button, or other widgets within your activity. Unless overidden, calling this function is the same as calling startSearch(null, false, null, false), which launches search for the current activity as specified in its manifest, see SearchManager.

You can override this function to force global search, e.g. in response to a dedicated search key, or to block search entirely (by simply returning false).

Your AndroidManifest.xml is also incomplete. You need to show that your Activity is searchable. Change your MainActivity declaration as follows:

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Note that @xml/searchable is an xml reference in your apk that defines how to search. This resource is discussed in detail here.

这篇关于Android的 - 如何禁用搜索按钮,如何实现onSearchRequested()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆