程序员人生 网站导航

[置顶] 安卓版actionSheet的实现

栏目:综合技术时间:2015-06-16 08:44:16

               actionSheet实现华丽的popWindow效果

            在这公司上班也是醉了,1个产品公司不大利于程序员的发展,最主要的是公司不关心员工的成长,每天就知道在公司优化代码和换下公司的界面等1些繁琐的事情,完全是在浪费时间,倒不如学1些新的东西,今天学ios的时候发现了qq5.0版的那个退出程序时的上弹提示菜单栏,之前也就是用popwindow来实现的,今天看ios的代码实现起来确切是如此的简单,也就是已封装好的1个控件UIActionSheet,想起安卓实现这功能是如此的操蛋,不能不觉得iphoe真的是有很大优势的,特别是去年6月出的swift语言,听说是比oc简单的多,学ios也是辛苦的,今天我也来实现1个安卓版的actionSheet。

先看下ios的实现这功能的截图和代码:

                                    

是否是很漂亮的界面,再看代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(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{
    //底部弹出dialog
    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"发送给好友" otherButtonTitles:@"转载到空间相册",@"上传到群相册",@"保存得手机",@"收藏",@"查看聊天图片",nil];
    [sheet showInView:self.window];
}

由因而初学者,所以用的是代码添加控件的方式可怜

然后下面是安卓来实现:

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;
/**
 * 自定义的actionSheet,有3部份组成(1.title   2.item  3.cancle按钮)
 * 当item 的条目多过8条时,item就可以转动显示
 * */
public class ActionSheet {
private Context context;
private Dialog dialog;
private TextView txt_title;//标题,默许是没有标题的
private TextView txt_cancel;//取消按钮
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;
// 获得window对象
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
//得到窗口的尺寸对象
display = windowManager.getDefaultDisplay();
}


// 为actionSheet构建自定义的控件
public ActionSheet builder() {
// 获得Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.view_actionsheet, null);
// 设置Dialog最小宽度为屏幕宽度
view.setMinimumWidth(display.getWidth());
// 获得自定义Dialog布局中的控件
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);

               //单独设置取消按钮的点击事件
txt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});


// 定义Dialog布局和参数
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;
}
    //为actionSheet设置标题
public ActionSheet setTitle(String title) {
showTitle = true;
txt_title.setVisibility(View.VISIBLE);
txt_title.setText(title);
return this;
}
// 设置dialog是不是能够取消
public ActionSheet setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}


// 设置dialog在屏幕外部是不是能够取消
public ActionSheet setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}


/**

* @param strItem
*            条目名称
* @param color
*            条目字体色彩,设置null则默许蓝色
* @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;
}


/** 设置条目布局 */
@SuppressWarnings("deprecation")
private void setSheetItems() {
if (sheetItems == null || sheetItems.size() <= 0) {
return;
}
int size = sheetItems.size();//除去title和cancle 按钮显示的总条目数
// 添加条目过量的时候控制高度
if (size >= 8) {//当条目过剩8条时就能够转动显示
LinearLayout.LayoutParams params = (LayoutParams) sLayout_content
.getLayoutParams();
//我这里设置的item的课显示总高度为屏幕高度的3/5
params.height = display.getHeight()*3 / 5;
sLayout_content.setLayoutParams(params);
}


// 循环添加条目
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);
// 背景图片
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);
}
}
}


// 字体色彩
if (color == null) {
textView.setTextColor(Color.parseColor(SheetItemColor.Blue
.getName()));
} else {
textView.setTextColor(Color.parseColor(color.getName()));
}


// 高度
float scale = context.getResources().getDisplayMetrics().density;//获得屏幕密度
int height = (int) (45 * scale + 0.5f);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height));//textView设置宽高


// 点击事件
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();
}

        //条目的接口回调类
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{//实现条目的点击
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("发送给好友", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("转载到空间相册", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("上传到群相册", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("保存得手机", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("收藏", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("查看聊天图片", SheetItemColor.Blue,
MainActivity.this);
actionSheet.show();
}
});
}
@Override
public void onClick(int which) {
Toast.makeText(MainActivity.this,"我被点击了"+which, 1).show();
}
}

弄了下,很简单的代码也就不需要介绍了,下面直接上图看效果

我也是醉了,截屏工具出问题,手机拍照的抓狂

源码呢,在这》》》》》》》戳actionSheet下载





------分隔线----------------------------
------分隔线----------------------------

最新技术推荐