Custom dialog ----- activity

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.