Android actionSheet implementation, Android actionsheet

Source: Internet
Author: User

Android actionSheet implementation, Android actionsheet
ActionSheet implements gorgeous popWindow Effects

Working in this company is also drunk, a product company is not very conducive to the development of programmers, the most important thing is that the company does not care about the growth of employees, every day, I know that some tedious things such as optimizing code and changing the company's interface are completely a waste of time. It is better to learn new things, today, when I was studying ios, I found that the menu bar was displayed when I exited the program of the qq5.0 version. It was previously implemented using popwindow, today, the ios code implementation is really so simple, that is, the encapsulated UIActionSheet control. It is so cool to think of Android's implementation of this function, I have to think that iphoe has a great advantage. Especially the swift language in last June is said to be much simpler than oc, and it is hard to learn ios, today, I also come to implement an android actionSheet.

Let's take a look at the implementation and code of this function in ios:


Is it a pretty interface? Let's look at the code again:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
Self. window. backgroundColor = [UIColor whiteColor];
[Self. window makeKeyAndVisible];
Self. button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Self. button. frame = CGRectMake (50,250,280, 50 );
[Self. button setTitle: @ "show actionSheet" forState: UIControlStateNormal];
[Self. button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
// Self. button. font = [UIFont fontWithName: @ "STHeiti-Medium.ttc" size: 10];
Self. button. backgroundColor = [UIColor blueColor];
[Self. button addTarget: self action: @ selector (click) forControlEvents: UIControlEventTouchUpInside];
[Self. window addSubview: self. button];
Return YES;
}
-(Void) click {
// The dialog pop-up at the bottom
UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "send to friend" otherButtonTitles: @ "reprint to space album ", @ "upload to group album", @ "Save to cell phone", @ "add to Favorites", @ "view chat image", nil];
[Sheet showInView: self. window];
}

Because it is a beginner, the code is used to add controls.

Then the following is the implementation of Android:

ActionSheet. java

Package com. zy. actionsheet;


Import java. util. ArrayList;
Import java. util. List;


Import android. app. Dialog;
Import android. content. Context;
Import android. graphics. Color;
Import android. view. Display;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. Window;
Import android. view. WindowManager;
Import android. widget. LinearLayout;
Import android. widget. LinearLayout. LayoutParams;
Import android. widget. ScrollView;
Import android. widget. TextView;
/**
* Custom actionSheet consists of three parts (1. title 2. item 3. cancle button)
* If there are more than eight entries in an item, the item will be displayed in scroll mode.
**/
Public class ActionSheet {
Private Context context;
Private Dialog dialog;
Private TextView txt_title; // Title. By default, no title is displayed.
Private TextView txt_cancel; // The cancel button.
Private LinearLayout lLayout_content;
Private ScrollView sLayout_content;
Private boolean showTitle = false;
Private List <SheetItem> sheetItems;
Private Display display;


Public ActionSheet (Context context ){
This. context = context;
// Obtain the window object
WindowManager windowManager = (WindowManager) context
. GetSystemService (Context. WINDOW_SERVICE );
// Obtain the size object of the window.
Display = windowManager. getDefaultDisplay ();
}


// Build custom controls for actionSheet
Public ActionSheet builder (){
// Obtain the Dialog Layout
View view = LayoutInflater. from (context). inflate (
R. layout. view_actionsheet, null );
// Set the minimum Dialog width to the screen width
View. setMinimumWidth (display. getWidth ());
// Obtain the control in the Custom Dialog Layout
SLayout_content = (ScrollView) view. findViewById (R. id. sLayout_content );
LLayout_content = (LinearLayout) view
. FindViewById (R. id. lLayout_content );
Txt_title = (TextView) view.findViewById(R.id.txt _ title );
Txt_cancel = (TextView) view.findViewById(R.id.txt _ cancel );

// Set the click event for the cancel button separately
Txt_cancel.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Dialog. dismiss ();
}
});


// Define the Dialog layout and Parameters
Dialog = new Dialog (context, R. style. ActionSheetDialogStyle );
Dialog. setContentView (view );
Window dialogWindow = dialog. getWindow ();
DialogWindow. setGravity (Gravity. LEFT | Gravity. BOTTOM );
WindowManager. LayoutParams lp = dialogWindow. getAttributes ();
Lp. x = 0;
Lp. y = 0;
DialogWindow. setAttributes (lp );
Return this;
}
// Set the title for actionSheet
Public ActionSheet setTitle (String title ){
ShowTitle = true;
Txt_title.setVisibility (View. VISIBLE );
Txt_title.setText (title );
Return this;
}
// Sets whether the dialog can be canceled.
Public ActionSheet setCancelable (boolean cancel ){
Dialog. setCancelable (cancel );
Return this;
}


// Set whether the dialog can be canceled outside the screen
Public ActionSheet setCanceledOnTouchOutside (boolean cancel ){
Dialog. setCanceledOnTouchOutside (cancel );
Return this;
}


/**
*
* @ Param strItem
* Entry name
* @ Param color
* The entry font color. If it is null, the default value is blue.
* @ Param listener
* @ Return actionSheet
*/
Public ActionSheet addSheetItem (String strItem, SheetItemColor color,
OnSheetItemClickListener listener ){
If (sheetItems = null ){
SheetItems = new ArrayList <SheetItem> ();
}
SheetItems. add (new SheetItem (strItem, color, listener ));
Return this;
}


/** Set the Entry Layout */
@ SuppressWarnings ("deprecation ")
Private void setSheetItems (){
If (sheetItems = null | sheetItems. size () <= 0 ){
Return;
}
Int size = sheetItems. size (); // remove the total number of entries displayed by the title and cancle buttons
// Control the height when too many entries are added
If (size> = 8) {// scroll when there are eight more entries
LinearLayout. LayoutParams params = (LayoutParams) sLayout_content
. GetLayoutParams ();
// The class display height of the item set here is 3/5 of the screen height.
Params. height = display. getHeight () * 3/5;
SLayout_content.setLayoutParams (params );
}


// Add entries cyclically
For (int I = 1; I <= size; I ++ ){
Final int index = I;
SheetItem sheetItem = sheetItems. get (I-1 );
String strItem = sheetItem. name;
SheetItemColor color = sheetItem. color;
Final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem. itemClickListener;
TextView textView = new TextView (context );
TextView. setText (strItem );
TextView. setTextSize (18 );
TextView. setGravity (Gravity. CENTER );
// Background image
If (size = 1 ){
If (showTitle ){
TextView. setBackgroundResource (R. drawable. actionsheet_bottom_selector );
} Else {
TextView. setBackgroundResource (R. drawable. actionsheet_single_selector );
}
} Else {
If (showTitle ){
If (I> = 1 & I <size ){
TextView. setBackgroundResource (R. drawable. actionsheet_middle_selector );
} Else {
TextView. setBackgroundResource (R. drawable. actionsheet_bottom_selector );
}
} Else {
If (I = 1 ){
TextView. setBackgroundResource (R. drawable. actionsheet_top_selector );
} Else if (I <size ){
TextView. setBackgroundResource (R. drawable. actionsheet_middle_selector );
} Else {
TextView. setBackgroundResource (R. drawable. actionsheet_bottom_selector );
}
}
}


// Font color
If (color = null ){
TextView. setTextColor (Color. parseColor (SheetItemColor. Blue
. GetName ()));
} Else {
TextView. setTextColor (Color. parseColor (color. getName ()));
}


// Height
Float scale = context. getResources (). getDisplayMetrics (). density; // obtain the screen density
Int height = (int) (45 * scale + 0.5f );
TextView. setLayoutParams (new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, height); // textView


// Click an event
TextView. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Listener. onClick (index );
Dialog. dismiss ();
}
});


LLayout_content.addView (textView );
}
}


Public void show (){
SetSheetItems ();
Dialog. show ();
}

// Interface callback class
Public interface OnSheetItemClickListener {
Void onClick (int which );
}


Public class SheetItem {
String name;
OnSheetItemClickListener itemClickListener;
SheetItemColor color;


Public SheetItem (String name, SheetItemColor color,
OnSheetItemClickListener itemClickListener ){
This. name = name;
This. color = color;
This. itemClickListener = itemClickListener;
}
}


Public enum SheetItemColor {
Blue ("# 037BFF"), Red ("# FD4A2E ");
Private String name;


Private SheetItemColor (String name ){
This. name = name;
}


Public String getName (){
Return name;
}


Public void setName (String name ){
This. name = name;
}
}
}

MainActivity. java

Package com. zy. actionsheet;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
Import com. zy. actionsheet. ActionSheet. OnSheetItemClickListener;
Import com. zy. actionsheet. ActionSheet. SheetItemColor;
Public class MainActivity extends Activity implements OnSheetItemClickListener {// click to implement entries
Private Button btn1;
Private ActionSheet actionSheet;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
InitView ();
}
Private void initView (){
Btn1 = (Button) findViewById (R. id. btn1 );
Btn1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
ActionSheet = new ActionSheet (MainActivity. this)
. Builder ()
. SetCancelable (false)
. SetCanceledOnTouchOutside (false)
. AddSheetItem ("send to friend", SheetItemColor. Blue,
MainActivity. this)
. AddSheetItem ("reprint to space album", SheetItemColor. Blue,
MainActivity. this)
. AddSheetItem ("upload to group album", SheetItemColor. Blue,
MainActivity. this)
. AddSheetItem ("Save to cell phone", SheetItemColor. Blue,
MainActivity. this)
. AddSheetItem ("favorites", SheetItemColor. Blue,
MainActivity. this)
. AddSheetItem ("view chat images", SheetItemColor. Blue,
MainActivity. this );
ActionSheet. show ();
}
});
}
@ Override
Public void onClick (int which ){
Toast. makeText (MainActivity. this, "I was clicked" + which, 1). show ();
}
}

After that, the simple code does not need to be introduced. The following describes the results.

I was drunk too. The screenshot tool went wrong. I took a photo on my mobile phone.

Source code: Download The actionSheet at this address.





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.