禁止在Android上的搜索按钮 [英] Disable the search button in Android

查看:176
本文介绍了禁止在Android上的搜索按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我不希望用户能够取消一个Android应用程序的对话框。使用 .setCancelable(假)禁用后退按钮,但pressing搜索按钮还是取消对话框。我看到了<一个href="http://stackoverflow.com/questions/7021739/disable-the-search-button-quick-search-box-in-android/7023495#comment12447188_7023495">this问题它告诉我,我应该包括

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

但我还是能够取消与搜索按钮关闭对话框。这是我的code:

 公共类TestActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
        公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的ShowDialog(0);
    }

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

    受保护的对话框onCreateDialog(INT ID){
        最后AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setMessage(消息)
               .setCancelable(假)
               .setPositiveButton(OK,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话框,INT ID){
                       //什么
                   }
               });
         返回builder.create();
    }
}
 

解决方案

@Benh你需要这个code,为您的对话按键侦听器设置

  builder.setOnKeyListener(的KeyListener);
 

添加下面code在活动课

  OnKeyListener的KeyListener =新DialogInterface.OnKeyListener(){
    @覆盖
    公共布尔onKey(DialogInterface对话,诠释键code,KeyEvent的事件){
        如果(键code == KeyEvent.KEY code_SEARCH和放大器;&安培; event.getRepeatCount()== 0){
            返回true; //我们停止开始取消对话框或进度
        }
        返回false;
    }
};
 

试试这上面的东西在你的对话,希望能够为您服务。

I have a dialog in an Android app that I don't want the user to be able to cancel. Using .setCancelable(false) disables the back button, but pressing the search button still cancels the dialog. I saw this question which told me that I should include

public boolean onSearchRequested() {
    return false;
}

But I'm still able to cancel the dialog with the search button. Here's my code:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showDialog(0);
    }

    public boolean onSearchRequested() {
        return false;
    }

    protected Dialog onCreateDialog(int id) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message")
               .setCancelable(false)
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // nothing
                   }
               });
         return builder.create();
    }
}

解决方案

@Benh You need this code to set for your Key Listener for Dialog

   builder.setOnKeyListener(keylistener);

Add Below code in your Activity Class

  OnKeyListener keylistener=new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
            return true; //we stop begin cancel of dialog or Progressbar
        }
        return false; 
    }
}; 

try this above thing in your dialog hope that will work for you.

这篇关于禁止在Android上的搜索按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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