您可以在警报对话框中捕获并处理搜索按键吗?
我想利用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将
OnKeyListener
附加到对话框,如下所示:You simply need to attach an
OnKeyListener
to the dialog like this: