Custom dialog ----- activity
Generally, the dialog style that comes with android does not meet your requirements. Therefore, you must customize the dialog style.
In fact, the activity can also exist in the form of dialog. We only need to set its theme attribute in the list file:
Android: name = ". EditActivity"
Android: theme = "@ android: style/Theme. Dialog"
>
The general dialog in the activity form will be dismiss when you touch the outside of it. So, how can we avoid dismiss it?
To achieve this, you only need one code:
SetFinishOnTouchOutside (false), which is a bit like setOutsideTouchable (false) of popupwindow );
To press the return key, you only need to block the return key or override the backpress method.
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
Return true;
}
Return super. onKeyDown (keyCode, event );
}
@ Override
Public void onBackPressed (){
/// By default, the finish () method is called.
/// Super. onBackPressed ();
}
//// This is the method that the activity originally intended to implement
Public void onBackPressed (){
If (! MFragments. popBackStackImmediate ()){
Finish ();
}
}
This is not enough. The activity is displayed in the form of dialog and the width will be small. We need to specify the width ourselves:
WindowManager manager = getWindowManager ();
DisplayMetrics displayMetrics = new DisplayMetrics ();
Display display = manager. getDefaultDisplay ();
Display. getMetrics (displayMetrics );
Int width = displayMetrics. widthPixels;
Int height = displayMetrics. heightPixels;
WindowManager. LayoutParams params = getWindow (). getAttributes ();
Params. width = (int) (width * 0.8 );
GetWindow (). setAttributes (params );
In this way, the activity width is set.