禁止在Android上的搜索按钮按钮、Android

2023-09-07 09:04:18 作者:孤独的心态

我有,我不希望用户能够取消一个Android应用程序的对话框。使用 .setCancelable(假)禁用后退按钮,但pressing搜索按钮还是取消对话框。我看到了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);
 
Android ImageButton 图片按钮

添加下面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.