Disable the search button in Android

前端 未结 4 368
眼角桃花
眼角桃花 2021-01-14 05:45

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 sear

相关标签:
4条回答
  • 2021-01-14 06:06
     public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.ACTION_DOWN==KeyEvent.KEYCODE_SEARCH)
            return false;
        else
        return super.onKeyDown(keyCode, event);
    }
    
    0 讨论(0)
  • 2021-01-14 06:07

    I disable search button by overriding progress dialog. I create unnamed class and override method onSearchRequest() . And this is working for me. I use this:

    progressDialog = new ProgressDialog(Activity.this){
    
                    @Override
                    public boolean onSearchRequested() {
                        return true;
                    }           
    
    
                };
    

    instead code:

    progressDialog = new ProgressDialog(Activity.this);
    
    0 讨论(0)
  • 2021-01-14 06:13

    You simply need to listen for search button presses and do nothing when they are hit.

    public boolean onKeyDown(int keycode, KeyEvent e) {
                switch(keycode) {
                    case KeyEvent.KEYCODE_SEARCH:
                        return true;
                        break;
                }
    
                return super.onKeyDown(keycode, e);
            }
    

    If this doesn't work for your Activity class then you'll probably need to create a subclass of Dialog and implement the onKeyDown method for your dialog class.

    0 讨论(0)
  • 2021-01-14 06:14

    @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.

    0 讨论(0)
提交回复
热议问题