下面列出了android.app.Dialog#setOnKeyListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
public FilterConfig(TableLayout table, Button categoryUp, Button categoryDn, TextView categoryField ) {
configTable = table;
editDialog = new Dialog(table.getContext(), R.style.Theme_dialog_TitleBar);
editDialog.setOnKeyListener(this);
editDialog.setContentView(R.layout.filterentryeditdialog);
editDialog.setTitle(table.getContext().getResources().getString(R.string.editFilterDialogTitle));
editOk = editDialog.findViewById(R.id.filterEditOkBtn);
editDelete = editDialog.findViewById(R.id.filterEditDelBtn);
editCancel = editDialog.findViewById(R.id.filterEditCancelBtn);
editOk.setOnClickListener(this);
editDelete.setOnClickListener(this);
editCancel.setOnClickListener(this);
this.categoryUp = categoryUp;
this.categoryDown = categoryDn;
this.categoryField = categoryField;
categoryField.setText(ALL_ACTIVE);
categoryMap = new TreeMap();
}
protected void setupDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStatusBarTranslucent(true);
} else {
setStatusBarTranslucent(presentableActivity.isStatusBarTranslucent());
}
Window window = getWindow();
if (window != null) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Dialog dialog = getDialog();
if (dialog != null) {
dialog.setOnKeyListener((dialogInterface, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
if (!dispatchBackPressed() && isCancelable()) {
hideDialog();
}
return true;
}
return false;
});
}
}
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(dialogInterface -> {
if (ViewHelper.isTablet(getActivity())) {
if (dialog.getWindow() != null) {
dialog.getWindow().setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
}
onDialogIsShowing();
});
dialog.setOnKeyListener((dialog1, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK) {
isAlreadyHidden = true;
onDismissedByScrolling();
}
return false;
});
return dialog;
}
@NonNull
public static DialogMaker makeDialog(Context context) {
DialogMaker dialogMaker = new DialogMaker();
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.gif_dialog);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
}
return true;
}
});
return dialogMaker;
}
public void initView(Context mContext) {
this.mContext = mContext;
SoundTool.initSound(mContext);
mDialog = new Dialog(mContext, R.style.dialogViewStyle);
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.dialog_view, null);
initDialogView(view);
mDialog.setContentView(view);
Window window = mDialog.getWindow();
// ���ö���
// window.setWindowAnimations(R.style.dialogViewAnim);
// ����
// window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.dialog_back));
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
// window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mDialog.setOnKeyListener(mOnKeyListener);
view.setOnHoverListener(mOnTouchListener);
// setView();
mHandler.sendEmptyMessage(1);
mHandler.sendEmptyMessage(7);
mHandler.sendMessage(mHandler.obtainMessage(9, 0, 0));
initUsbData();
}
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
// hides the actual dialog title, we have one already...
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// we want to listen to clicks on back button
dialog.setOnKeyListener(this);
return dialog;
}
public void init(AttributeSet attrs) {
// We create a fake dialog which dims the screen and we display the expandable menu as content
mDialog = new Dialog(getContext(), android.R.style.Theme_Translucent_NoTitleBar);
mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
lp.dimAmount = dimAmount;
mDialog.getWindow().setAttributes(lp);
mButtonMenu = new ExpandableButtonMenu(getContext(), attrs);
mButtonMenu.setButtonMenuParentOverlay(this);
mDialog.setContentView(mButtonMenu);
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
setVisibility(View.INVISIBLE);
mButtonMenu.toggle();
}
});
// Catch events when keyboard button are clicked. Used to dismiss the menu
// on 'back' button
mDialog.setOnKeyListener(this);
// Clicking this view will expand the button menu
setOnClickListener(this);
}
public Dialog doCaptureKey() {
final Dialog d = new Dialog(_gamePadActivity);
d.setContentView(R.layout.prefs_capture_key);
d.setCanceledOnTouchOutside(true);
d.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
_setActionKey(keyCode);
d.dismiss();
return true;
}
});
Button b = d.findViewById(R.id.buttonResetToDefault);
b.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (_captureKey) {
case PICK_UP:
_setActionKey(KeyEvent.KEYCODE_BUTTON_Y);
break;
case JUMP:
_setActionKey(KeyEvent.KEYCODE_BUTTON_A);
break;
case PUNCH:
_setActionKey(KeyEvent.KEYCODE_BUTTON_X);
break;
case BOMB:
_setActionKey(KeyEvent.KEYCODE_BUTTON_B);
break;
case RUN1:
_setActionKey(KeyEvent.KEYCODE_BUTTON_L1);
break;
case RUN2:
_setActionKey(KeyEvent.KEYCODE_BUTTON_R1);
break;
case START:
_setActionKey(KeyEvent.KEYCODE_BUTTON_START);
break;
default:
LogThread.log("Error: unrecognized key in doActionKey", null,
d.getContext());
break;
}
d.dismiss();
}
});
d.setTitle(R.string.capturing);
d.show();
return d;
}
@Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
Window window = d.getWindow();
window.setLayout(width, height);
window.setWindowAnimations(R.style.dialogFragmentAnimation);
d.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (BuildConfig.DEBUG)
Timber.d("onKey: %d (Back=%d). Event:%d (Down:%d, Up:%d)", keyCode, KeyEvent.KEYCODE_BACK, event.getAction(),
KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
switch (actualFormState) {
case REGISTRATION:
if (event.getAction() == KeyEvent.ACTION_UP) {
setVisibilityOfRegistrationForm(false);
}
return true;
case FORGOTTEN_PASSWORD:
if (event.getAction() == KeyEvent.ACTION_UP) {
setVisibilityOfEmailForgottenForm(false);
}
return true;
case EMAIL:
if (event.getAction() == KeyEvent.ACTION_UP) {
setVisibilityOfEmailForm(false);
}
return true;
default:
return false;
}
}
return false;
}
});
}
}