DialogFragment常見問題(黑色稜角、點選編輯框外關閉軟鍵盤..)
阿新 • • 發佈:2019-01-31
1、設定對話方塊外部的背景為完全透明:
在onStart()方法中加入以下程式碼:
@Override public void onStart() { super.onStart(); /** * 將對話方塊外部(未被遮擋的部分)的背景設定為透明(必須設定,業務需要) */ Window window = getDialog().getWindow(); WindowManager.LayoutParams windowParams = window.getAttributes(); windowParams.dimAmount =BRIGHTNESS_OVERRIDE_OFF; window.setAttributes(windowParams); setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen); }
2、去掉,彈窗的黑色稜角
在onViewCreated方法中加入:
getDialog().getWindow().setBackgroundDrawable(new BitmapDrawable());
3、設定軟鍵盤監聽,在DialogFragment中含有EditText時,點選EditText以外的部分,關閉軟鍵盤;
在onViewCreate()方法中加入如下程式碼:
/** * 點選非輸入框區域時,自動收起鍵盤 */ private void initSoftInputListener() { getDialog().getWindow().getDecorView() .setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { InputMethodManager manager = (InputMethodManager)getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); if (event.getAction() == MotionEvent.ACTION_DOWN) { if (getDialog().getCurrentFocus() != null && getDialog().getCurrentFocus().getWindowToken() != null) { manager.hideSoftInputFromWindow( getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } return false; } }); }