您可以在警报对话框中捕获并处理搜索按键吗?

发布于 2024-12-08 17:39:13 字数 2294 浏览 0 评论 0原文

我想利用 Androids AlertDialog 构建器,但也要在用户按下搜索键时捕获。我可以在自定义对话框中执行此操作,但无法弄清楚如何在警报中执行此操作。这是我的代码,其中注释掉了自定义部分(我没有自定义布局):

 public static boolean show(final Activity activity) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);


    if (!preferences.getBoolean(ACCUWX.Preferences.PREFERENCE_EULA_ACCEPTED, false)) {
     final TextView message = new TextView(activity);
     final SpannableString s =
     new SpannableString(activity.getText(R.string.eula_terms_info));
     Linkify.addLinks(s, Linkify.WEB_URLS);
     message.setPadding(10, 10, 10, 10);
     message.setText(s);
     message.setTextColor(Color.GRAY);
     message.setMovementMethod(LinkMovementMethod.getInstance());



        mAd = new AlertDialog.Builder(activity);
        mAd.setTitle(R.string.terms_conditions);
        mAd.setCancelable(false);
        mAd.setPositiveButton(R.string.agree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                accept(preferences);
                if (activity instanceof OnEulaAgreedTo) {
                    ((OnEulaAgreedTo) activity).onEulaAgreedTo();
                }
            }
        })
        .setNegativeButton(R.string.disagree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                reject(activity);
            }
        })
        .setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                refuse(activity);
            }
        })

        .setView(message)
        .create();

        mAd.show();

        return false;
    }
    return true;
}

这是对话框:

  Dialog dialog = new Dialog(activity) {
 @Override 
public boolean onKeyDown(int keyCode, 
                KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_SEARCH) 
        { 
                System.out.println("----- ignore search pressed"); 
                return true; 
        } 
        return false; 
}
};

dialog.setContentView(R.layout.terms_and_conditions_dialog);
dialog.setTitle(R.string.terms_conditions);
dialog.setCancelable(false);
dialog.show();

I want to utilize Androids AlertDialog builder, but also catch when user presses search key. I can do it in a custom dialog, but cannot figure how to do in alert. Here is my code with Custom section commented out (I have no layout for custom):

 public static boolean show(final Activity activity) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);


    if (!preferences.getBoolean(ACCUWX.Preferences.PREFERENCE_EULA_ACCEPTED, false)) {
     final TextView message = new TextView(activity);
     final SpannableString s =
     new SpannableString(activity.getText(R.string.eula_terms_info));
     Linkify.addLinks(s, Linkify.WEB_URLS);
     message.setPadding(10, 10, 10, 10);
     message.setText(s);
     message.setTextColor(Color.GRAY);
     message.setMovementMethod(LinkMovementMethod.getInstance());



        mAd = new AlertDialog.Builder(activity);
        mAd.setTitle(R.string.terms_conditions);
        mAd.setCancelable(false);
        mAd.setPositiveButton(R.string.agree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                accept(preferences);
                if (activity instanceof OnEulaAgreedTo) {
                    ((OnEulaAgreedTo) activity).onEulaAgreedTo();
                }
            }
        })
        .setNegativeButton(R.string.disagree, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                reject(activity);
            }
        })
        .setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                refuse(activity);
            }
        })

        .setView(message)
        .create();

        mAd.show();

        return false;
    }
    return true;
}

Here is Dialog:

  Dialog dialog = new Dialog(activity) {
 @Override 
public boolean onKeyDown(int keyCode, 
                KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_SEARCH) 
        { 
                System.out.println("----- ignore search pressed"); 
                return true; 
        } 
        return false; 
}
};

dialog.setContentView(R.layout.terms_and_conditions_dialog);
dialog.setTitle(R.string.terms_conditions);
dialog.setCancelable(false);
dialog.show();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-12-15 17:39:13

您只需将 OnKeyListener 附加到对话框,如下所示:

mAd.setOnKeyListener(new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KEYCODE_SEARCH)  {
                        // Handle search key
                        return true;
            }
            return false;
        }
    })
.create();

You simply need to attach an OnKeyListener to the dialog like this:

mAd.setOnKeyListener(new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KEYCODE_SEARCH)  {
                        // Handle search key
                        return true;
            }
            return false;
        }
    })
.create();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文