Android ios-like pop-up box

Source: Internet
Author: User

Android ios-like pop-up box

Preparation:

Public class ActionSheet {
Public interface OnActionSheetSelected {
Void onClick (int whichButton );
}

Private ActionSheet (){
}

Public static Dialog showSheet (final Context context, final int layoutId,
Final OnActionSheetSelected actionSheetSelected,
Final OnCancelListener cancelListener ){
Final Dialog dialog = new Dialog (context, R. style. ActionSheet );
Final LayoutInflater inflater = (LayoutInflater) context
. GetSystemService (Context. LAYOUT_INFLATER_SERVICE );
Final LinearLayout layout = (LinearLayout) inflater. inflate (layoutId,
Null );
Final int cFullFillWidth = 10000;
Layout. setMinimumWidth (cFullFillWidth );

Final Window w = dialog. getWindow ();
Final WindowManager. LayoutParams lp = w. getAttributes ();
Lp. x = 0;
Final int cMakeBottom =-1000;
Lp. y = cMakeBottom;
Lp. gravity = Gravity. BOTTOM;
Dialog. onalog wattributeschanged (lp );
Dialog. setCanceledOnTouchOutside (true );
If (cancelListener! = Null ){
Dialog. setOnCancelListener (cancelListener );
}

Dialog. setContentView (layout );
Dialog. show ();

Return dialog;
}
}

<Style name = "ActionSheet" parent = "@ android: style/Theme. Dialog">

Application:

Private ActionSheet. OnActionSheetSelected mOnActionSheetSelected;
private Dialog mActionSheet;

Call the showGetPhotoDialog () method:

public void onActionSheetClicked(final View view) {   mOnActionSheetSelected.onClick(view.getId());}public void setOnSheetClicked(final ActionSheet.OnActionSheetSelected onActionSheetSelected) {   mOnActionSheetSelected = onActionSheetSelected;}private void showGetPhotoDialog() {   mActionSheet = ActionSheet.showSheet(this, R.layout.actionsheet_dialog, getOnActionSheetClicked(), null);}private ActionSheet.OnActionSheetSelected getOnActionSheetClicked() {   return new ActionSheet.OnActionSheetSelected() {      @Override      public void onClick(final int whichButton) {         switch (whichButton) {            case R.id.tv_button1:                              break;
     case R.id.tv_button2:                              break;
Case R. id.TV _calendar:
MActionSheet. dismiss ();
Break;
Default:
Break;
}
MActionSheet. dismiss ();}
};
}

Layout:

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ color/clear"
Android: orientation = "vertical"
Android: padding = "5dp">

<TextView
Android: id = "@ + id/TV _button1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/actionsheet_top_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "button 1"
Android: textSize = "18sp"/>

<TextView
Android: id = "@ + id/TV _button2"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/actionsheet_bottom_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "button 2"
Android: textColor = "@ color/green_title_bar"
Android: textSize = "18sp"/>

<TextView
Android: id = "@ + id/cancel"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "10dp"
Android: background = "@ drawable/actionsheet_single_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "@ string/cancle"
Android: textColor = "@ color/cancle_blue"
Android: textSize = "18sp"/>

</LinearLayout>

Related Article

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.